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?
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.
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);
}
}