how to make camera follow player after a delay
So, basically I want the camera to be steady. Then the player like "walks into the frame", and then the camera should follow him from that point on. So, I want the camer ato follow the player after a dealy, or period of time. I found this script in a different thread, but when I apply it to my camera and apply my "Player" to "player", the camera just jumps to the player after 0.5 seconds, and that repeats itself over and over again. So I want the camera to jump to the player after like 2 seconds, and then follow him constantly.
This is the script from the other thread:
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
public float delay = 0.5f;
private float followNow;
void Start()
{
followNow = Time.time + delay;
offset = transform.position;
}
void LateUpdate ()
{
if (Time.time > followNow) {
if (player != null) {
transform.position = player.transform.position + offset;
}
followNow += delay;
}
}
}
How can I achieve my goal?
Answer by Hellium · Apr 22, 2018 at 11:30 AM
using UnityEngine;
using System.Collections.Generic;
public class DelayedCameraFollower : MonoBehaviour
{
private struct PointInSpace
{
public Vector3 Position ;
public float Time ;
}
[SerializeField]
[Tooltip("The transform to follow")]
private Transform target;
[SerializeField]
[Tooltip("The offset between the target and the camera")]
private Vector3 offset;
[Tooltip("The delay before the camera starts to follow the target")]
[SerializeField]
private float delay = 0.5f;
[SerializeField]
[Tooltip("The speed used in the lerp function when the camera follows the target")]
private float speed = 5;
///<summary>
/// Contains the positions of the target for the last X seconds
///</summary>
private Queue<PointInSpace> pointsInSpace = new Queue<PointInSpace>();
void LateUpdate ()
{
// Add the current target position to the list of positions
pointsInSpace.Enqueue( new PointInSpace() { Position = target.position, Time = Time.time } ) ;
// Move the camera to the position of the target X seconds ago
while( pointsInSpace.Count > 0 && pointsInSpace.Peek().Time <= Time.time - delay + Mathf.Epsilon )
{
transform.position = Vector3.Lerp( transform.position, pointsInSpace.Dequeue().Position + offset, Time.deltaTime * speed);
}
}
}
That works! BUT, now when the camera follows the player, the player like "glitches". It wobbles around and the higher the speed, the more it wobbles.
It's hard to diagnose the problem without the player script.
I only have a little bit of code for the movement, since the player only moves left and right
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public Rigidbody rb;
public float FloatForce = 600f;
public float LeftRightForce = 1000f;
// Update is called once per frame
void FixedUpdate ()
{
rb.AddForce(0, 0, FloatForce * Time.deltaTime);
if(Input.Get$$anonymous$$ey ("d"))
{
rb.AddForce(LeftRightForce * Time.deltaTime, 0, 0);
}
if (Input.Get$$anonymous$$ey("a"))
{
rb.AddForce(-LeftRightForce * Time.deltaTime, 0, 0);
}
}
}