- Home /
Question by
zak666 · Jul 25, 2020 at 01:22 AM ·
camera-movement
objects look jittery when camera moves?
Hey everyone, I put a cmaerascript to make my caerma lag a bit behind the player so it looks and feels nicer but the player now has a jittery movement , how do I resolve this I've tried difrent update methods n that but cant figure it out (camera script n player script not mine. XD)
Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerSmupController2 : MonoBehaviour
{
public float xMin, xMax, zMin, zMax;
public float speed;
public float tilt;
public Rigidbody rigidbodyRB;
public GameObject CameraCamObj;
public void Start()
{
CameraCamObj.transform.parent = null;
}
void Update()
{
float moveHorizontal = SimpleInput.GetAxis("Horizontal");
float moveVertical = SimpleInput.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbodyRB.velocity = movement * speed;
rigidbodyRB.position = new Vector3
(
Mathf.Clamp(rigidbodyRB.position.x, xMin, xMax),
0.0f,
Mathf.Clamp(rigidbodyRB.position.z, zMin, zMax)
);
rigidbodyRB.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbodyRB.velocity.x * -tilt);
}
}
Player: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerCameracam : 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 Update()
{
// 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);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
camera movements very slow, why!? :) 2 Answers
How can you make a FPS controller camera "fly"? 1 Answer
Mouse Camera Control 0 Answers
Teleporting a camera to a camera 1 Answer
Camera 2D Movement android unity 1 Answer