N
N
NikeRumer2022-03-12 16:25:19
C++ / C#
NikeRumer, 2022-03-12 16:25:19

How to make an object react only to a vertical swipe?

There is a code that moves an object along the z axis. Is there a way to make code using OnDrag so that it fixes the position of the swipe and reacts to the vertical one?

public void FixedUpdate()
    {
        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);

            switch (touch.phase)
            {
                case TouchPhase.Began:
                    _startPos = touch.position;
                    break;

                case TouchPhase.Moved:
                    var dir = touch.position - _startPos;
                    var pos = transform.position + new Vector3(transform.position.x, transform.position.y, dir.y);
                    transform.position = Vector3.Lerp(transform.position, pos, Time.deltaTime * _speed);
                    break;
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2022-03-12
@HemulGM

Well, probably not to move to the value of the vector, on which you do not want to move?

K
KraGen.Developer, 2022-03-12
@KraGenDeveloper

everywhere in the transform position at the end add .z
transform.position.z
this would only change the Z position

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question