1
votes

I am using Vector3.Lerp in unity game to simply move a gameobject from one position to other smoothly. Below is my code:

public class playermovement : MonoBehaviour {


    public float timeTakenDuringLerp = 2f;

    private bool _isLerping;

    private Vector3 _startPosition;
    private Vector3 _endPosition;

    private float _timeStartedLerping;

    void StartLerping(int i)
    {
        _isLerping = true;
        _timeStartedLerping = Time.time  ; // adding 1 to time.time here makes it wait for 1 sec before starting

        //We set the start position to the current position, and the finish to 10 spaces in the 'forward' direction
        _startPosition = transform.position;
        _endPosition = new Vector3(transform.position.x + i,transform.position.y,transform.position.z);

    }

    void Update()
    {
        //When the user hits the spacebar, we start lerping
        if(Input.GetKey(KeyCode.Space))
        {
            int i = 65;
            StartLerping(i);
        }
    }

    //We do the actual interpolation in FixedUpdate(), since we're dealing with a rigidbody
    void FixedUpdate()
    {
        if(_isLerping)
        {
            //We want percentage = 0.0 when Time.time = _timeStartedLerping
            //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
            //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
            //"Time.time - _timeStartedLerping" is.
            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

            //Perform the actual lerping.  Notice that the first two parameters will always be the same
            //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
            //to start another lerp)
            transform.position = Vector3.Lerp (_startPosition, _endPosition, percentageComplete);

            //When we've completed the lerp, we set _isLerping to false
            if(percentageComplete >= 1.0f)
            {
                _isLerping = false;
            }
        }
    }


}

The code works fine and the gameobject moves smoothly between two points. But it takes about 1 sec to reach destination. I want to make it move faster. I have tried decreasing the value of float timeTakenDuringLerp but the speed isn't affected. I have followed this tutorial and the explanation there also says to change timeTakenDuringLerp variable in order to change speed but its not working here.

Any Suggestions please?

2
multiply percentageComplete value with a speed value like, transform.position = Vector3.Lerp (_startPosition, _endPosition, percentageComplete*speed); So when you increase speed, it will go faster. It is not a solid solution tho if you are using percentageComplete elsewhereThalthanas
@Thalthanas Thanks a lot. It worked perfectlyDigi Art

2 Answers

2
votes

H℮y, thanks for linking to my blog!

Decreasing timeTakenDuringLerp is the correct solution. That reduces the time it takes for the object to move from start to finish, which is another way of saying "it increases the speed".

If there is a specific speed you want the object to move at, you'll need to make timeTakenDuringLerp a variable rather than a constant, and set it to distance/speed. Or better yet, don't use Lerp at all, and instead set the object's velocity and let Unity's physics engine take care of it.

Multiplying percentageComplete by a constant, as suggested by @Thalthanas, is incorrect. That causes the lerping updates to continue occurring after the lerping has completed. It also makes the code hard to understand because timeTakenDuringLerp is no longer the time taken during the lerp.

I've double-checked with my code and it does indeed work, so the problem you are experiencing must be elsewhere. Or maybe you accidentally increased the time, which would decrease the speed?

0
votes

The solution is to

multiply percentageComplete value with a speed value like,

transform.position = Vector3.Lerp (_startPosition, _endPosition, percentageComplete*speed); 

So when you increase speed, it will go faster.