- Home /
Laggy Camera
Hello!
I recently changed my camera script, and the new one is really laggy.
Here is the script:
using UnityEngine;
using System.Collections;
public class CarCamera : MonoBehaviour
{
[SerializeField]
private float distanceAway;
[SerializeField]
private float distanceUp;
[SerializeField]
private float smooth;
[SerializeField]
private Transform follow;
private Vector3 targetPosition;
void Start() {
follow = GameObject.FindWithTag ("Player").transform;
}
void OnDrawGizmos () {
}
void LateUpdate ()
{
targetPosition = follow.position + follow.up * distanceUp - follow.forward * distanceAway;
Debug.DrawRay (follow.position, Vector3.up * distanceUp, Color.red);
Debug.DrawRay (follow.position, -1f * follow.forward * distanceAway, Color.blue);
Debug.DrawLine (follow.position, targetPosition, Color.magenta);
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
transform.LookAt (follow);
}
}
And here is the inspector:
So undo your changes and go back to your old camera script...?
Is your player being moved in FixedUpdate(), by any chance? If so, change your LateUpdate() code to FixedUpdate() as well to keep them in sync.
The new camera would be miles better than the old one if it worked correctly.
And yes I'll try the FixedUpdate()!
EDIT: It is laggy even with FixedUpdate() :(
In case it helps, this is my Car's FixedUpdate()
void FixedUpdate ()
{
currentSpeed = 2 * 22 / 7 * wheelBL.radius * wheelBL.rpm * 60 / 100;
currentSpeed = $$anonymous$$athf.Round (currentSpeed);
if (currentSpeed <= 0) {
reverseButton.GetComponent<CanvasGroup> ().alpha = 1;
reverseButton.GetComponent<CanvasGroup> ().interactable = true;
reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = true;
} else {
reverseButton.GetComponent<CanvasGroup> ().alpha = 0;
reverseButton.GetComponent<CanvasGroup> ().interactable = false;
reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = false;
}
if (gasOn) {
started = true;
Gas ();
}
if (reverseOn) {
started = true;
Reverse ();
}
}
You could also move the car by its velocity and enable interpolation mode on the rigidbody. That syncs the rendering too.
Answer by hexagonius · Feb 19, 2015 at 06:48 PM
I'd recommend usingSmoothDamp As it gives you control of how fast the camera "springs" towards the car like a rubber band.
Read about Lerp again. t needs to be a value interpolating from 0 to 1. You used Time.deltaTime * smooth as a value, which is a very jittery constant somewhere near 0 (e.g.: 0.05 times 4 = 0.2)
It is lagging even if I change
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
to
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smooth);
Save the canvasgroup to some variable on start and use it ins$$anonymous$$d of all the GetComponent calls
That didn't cause any issues with the previous camera. :( But yes I will do that.
Answer by ehsan_mohammadi · Apr 23, 2018 at 06:54 AM
Hi!
Change LateUpdate () function to FixedUpdate() and probably it's work.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Camera turns to the front when reversing car 1 Answer
Reverse Camera Twitches 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers