A
A
artshelom2019-01-09 15:39:30
C++ / C#
artshelom, 2019-01-09 15:39:30

How to move only within one image?

I have a picture inside it there is another picture, how to find out the borders of the parent picture? So that she does not go beyond the boundaries of the parent?
Here is the code I have:

if (Input.touchCount > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
                center = Input.touches[0].position;
            if (Input.touches[0].phase == TouchPhase.Moved || Input.touches[0].phase == TouchPhase.Stationary)
            {
                Vector2 moved = Input.touches[0].position - center; // вектор сдвига от начала касания до текущей точки касания
                if (transform.position.x <= 0.5 && transform.position.x >= -0.5)
                {
                    transform.position = new Vector3(transform.position.x + moved.x / 1000, transform.position.y, transform.position.z);
                }
            }
        }

But I only move in the middle of the screen, along the length of the parent. How can I make sure that the position of the parent is taken into account?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-01-09
@artshelom

where is it in your code that "according to the length of the parent" is present at all, please enlighten??
I don't see any reference to the parent (or target) object at all.
what are the pictures? Canvas?? Sprite? Quad??
In principle .. as an option to look to your side ..
https://docs.unity3d.com/ScriptReference/Mesh-boun...
https://docs.unity3d.com/ScriptReference/Renderer-...
for RectTransform probably it’s easier just by coordinates (change all these shifts from left to right, top to bottom, knowing the size of the parent)) ...
but again, you don’t have screenshots or examples) it’s not really clear)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question