Vector3.Lerp not working to move the player

Hello!

I’m doing something really simple. Basically, I’m landing a player from the air onto a runway. First, the player enters the “Landing zone”. Then this script takes over controls and lands the player. Here it is:

using UnityEngine;
using System.Collections;

public class LandThePlayer : MonoBehaviour {

    public GameObject PlayerCamera;
    public GameObject LandCamera;
    public GameObject Player;
    public GameObject LandTrigger;
    public GameObject StopPosition;
    public bool PlayerIsLanding;
    public float LandingTime;
    // Use this for initialization
    void Start ()
    {
    }
  
    // Update is called once per frame
    void Update ()
    {
        if(PlayerIsLanding == true)
        {
            LandPlayer();
        }
    }

    //I have a trigger detecting when the player enters a zone to start the landing
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            SwitchCameras();
            PlayerIsLanding = true;
            Debug.Log("Starting to land the player");
        }
    }

    void SwitchCameras()
    {
        //this part simply turns off the player's camera and turns on a landing camera
        //this is totally for looks and doesn't affect anything else
        PlayerCamera.gameObject.SetActive(false);
        LandCamera.gameObject.SetActive(true);
    }

    void LandPlayer()
    {
        //PlayerFromPoint is where the player is starting to land
        Vector3 PlayerFromPoint = LandTrigger.transform.position;
        //StopPosition is the end of the player's landing
        Vector3 PlayerToPoint = StopPosition.transform.position;
        //let's move the player
        Player.transform.position = Vector3.Lerp(PlayerFromPoint, PlayerToPoint, Time.deltaTime);
        //and let's spam the console
        Debug.Log("Player is landing");
    }
}

Everything should be working, but the player simply bounces around the PlayerFromPoint, not moving any closer to the PlayerToPoint. What could be wrong with this script? Or should I try something else to achieve the same result?

Thanks for your input!

P.S. If you’re wondering how I got this code:

Read more on how lerp works.

The third argument to lerp represents the percentage of the distance between the start and the end. If you pass 0 to it, you’ll get the start point and if you pass 1 to it, you’ll get the end point.

You’re passing Time.deltaTime. DeltaTime is the amount of time it took for the last frame to complete. If you’re consistently running at 60 FPS, then this value is going to be some small number: 0.01666…

You’re always passing Lerp that small number. If you always pass it the same number, it will result in the same percentage of the distance between start and end. Always passing 0.1 is always going to be 10% of the distance.

Instead, you want to pass a t value that increases between 0 to 1 over time. You could simply pass a variable, t, and then add deltaTime to that variable each frame.

3 Likes

I suspect you may want to take this:

Vector3 PlayerFromPoint = LandTrigger.transform.position;

and change it to this:

Vector3 PlayerFromPoint = Player.transform.position;

Then, your lerping will be roughly equivalent to this:

Player.transform.position = Vector3.Lerp(Player.transform.position, PlayerToPoint, .01666666666f);

That should actually bring you closer and closer to the destination.

It is a somewhat confusing usage of Lerp, but it seems somewhat in line with what the video was showing.

The video did in fact pass Time.deltaTime as the third parameter (visible at 2:13). As far as I can tell, it
is an official Unity video.

Thanks for all the input. While this script isn’t ideal, it seems to do the job well enough:

using UnityEngine;
using System.Collections;

public class LandThePlayer : MonoBehaviour {

    public GameObject PlayerCamera;
    public GameObject LandCamera;
    public GameObject Player;
    public Transform LandTrigger;
    public Transform StopPosition;
    public bool PlayerIsLanding;
    public float LandingTime;
    private float fracJourney;

    //TESTING FROM: https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
    public float speed = 1.0F;
    private float startTime;
    private float journeyLength;
    // Use this for initialization
    void Start ()
    {
        fracJourney = 0;
        startTime = Time.time;
        journeyLength = Vector3.Distance(LandTrigger.position, StopPosition.position);
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
        if (PlayerIsLanding == true)
        {
            LandPlayer();
        }
    }

    //I have a trigger detecting when the player enters a zone to start the landing
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            SwitchCameras();
            //FIX!
            PlayerIsLanding = true;
            Debug.Log("Starting to land the player");
        }
    }

    void SwitchCameras()
    {
        //this part simply turns off the player's camera and turns on a landing camera
        //this is totally for looks and doesn't affect anything else
        PlayerCamera.gameObject.SetActive(false);
        LandCamera.gameObject.SetActive(true);
    }

    void LandPlayer()
    {
        float distCovered = (Time.time - startTime) * speed;
        fracJourney = distCovered / journeyLength;
        Player.transform.position = Vector3.Lerp(LandTrigger.position, StopPosition.position, fracJourney);
    }
}

Does your player have a rigidbody?
If so, you should not be setting the transform position. You should use rigidbody.MovePosition

If it doesn’t have a rigidbody, you should not be using FixedUpdate to move the player. You should use
void Update() instead of FixedUpdate()

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

Vector3 newPos;
Quaternion newRot;

public Transform curPos;
public Transform front;
public Transform back;
public Transform right;
public Transform left;

public int mSmooth;
public int rSmooth;

void Start() {
newPos = transform.position;
newRot = transform.rotation;
}
void Update() {
if (Input.GetKeyDown (KeyCode.W)) {
newPos = front.position;
}

if (Input.GetKeyDown (KeyCode.S)) {
newPos = back.position;
}

if (Input.GetKeyDown (KeyCode.D)) {
newRot = right.rotation;
}

if (Input.GetKeyDown (KeyCode.A)) {
newRot = left.rotation;
}

transform.position = Vector3.Lerp (transform.position, newPos, Time.deltaTime * mSmooth);
transform.rotation = Quaternion.Lerp (transform.rotation, newRot, Time.deltaTime * rSmooth);
}
}

Create a new post describing your issue and please use code tags: https://forum.unity3d.com/threads/using-code-tags-properly.143875/

Vector 3 works if the 3rd parameter is going from 0 to 1. My last post in this thread has functioning code.