Hello dear friends. The question is: An object is twitching strongly when moving. If you remove if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) from the code, then the object moves smoothly as needed. How to realize a smooth movement after touching the screen?

  public Transform target; private Vector3 _startPos; private Vector3 _endPos; void Start() { _startPos = transform.position; _endPos = target.position; } void Update() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) transform.position = Vector3.Lerp(_startPos, _endPos, Time.time); } 

    2 answers 2

    To smooth motion between frames, Time.deltaTime is usually used - this is the time during which the frame was processed. Well, apparently, you did not quite understand how interpolation works.

    The Vector3.Lerp method returns the position from the starting point to the ending point according to t. However, t must be between 0 and 1, i.e. if you pass Vector3.Lerp(_startPos, _endPos, 0.5f); you will get a position exactly in the middle.

    Thus t - you need to count and most often it is the ratio of the time that has passed from the beginning of the movement to the time for which your "character" has to go all the way.

    I would suggest you use Vector3.MoveTowards . He takes from you two positions and the maximum offset that you allow him to make. In this case, you do not have to calculate the time and you can control the speed of the character. For example, like this:

     [SerializeField] float _velocity = 10; void Update() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) transform.position = Vector3.MoveTowards(transform.position, _endPos, Time.deltaTime * _velocity); } 

    In this implementation, your character will move from its current position towards the target point until it reaches this point, or until you remove your finger from the screen.

    Speed ​​can be controlled by changing the _velocity variable.

    • Well, in this case, you can also use LerpUnclamped :) - RiotBr3aker
    • @ RiotBr3aker, did not quite understand where and why? z oo - M. Green
    • instead of the usual interpolation, it will work with Time.time. - RiotBr3aker
    • @ RiotBr3aker, so, doesn’t it lead to the fact that he goes to the side of the point and just passes by at some point? - M. Green
    • Sorry, did not think about the question. I thought that a person uses 2 positions as guides. Then yes, Time.time here in principle will not work. - RiotBr3aker

    I think that the problem in Time.time is that it returns the time from the beginning of the frame. In fact, with each tach it will be different. I think it's worth digging this way. After all, if there is no delay in the form of a wheel, then everything goes fine. It seems to me. Try to specify some stable time. Like a second. If there is a norm, then exactly the problem with time. And as an option that he simply does not have enough time for action. Because of the condition. After all, as soon as the touch goes into another phase, the condition will not work