- Home /
how do you add camera lag or delay for flight sim
Hi, I am pretty new to scripting and unity but have done some tutorials etc. I am trying to make a flight sim and have made the movement controller for the plane without too many problems. My problem is that the plane looks fixed in the viewport so I want to add some delay to the camera instead of just parenting it to the plane. I have written this script and attached it to the camera:
Code:
var targetObject : Transform;
function Update() {
Follow();
}
function Follow () {
transform.position = targetObject.position;
transform.rotation = targetObject.rotation;
}
I have tried using - yield WaitForSeconds(1); - with the script but it just seems to make the camera wait 1 second and jump behind the plane. Can anyone help me add some lag to the script or suggest another way of achieving camera lag? Thanks
Answer by Rodin · May 30, 2011 at 03:59 PM
We will need a way to store the route of the plane for the last second or so, and 'playback' this route to the camera, with some delay.
To get you started, lets create two arrays in which we will store the position and rotation of the plane for each point in time. And a variable controlling how much lag the camera will have (in frames for the time being):
var prevPositions = Array();
var prevRotations = Array();
var cameraLag = 100;
Then, in the Update
function, lets append the current position and rotation to the arrays, and retrieve the position and rotation as it was 100 frames ago:
function Update() {
prevPositions.Push(targetObject.position);
prevRotations.Push(targetObject.rotation);
if(prevPositions.length > cameraLag) {
// .Shift() returns and removes the first element in an array
transform.position = prevPositions.Shift();
transform.rotation = prevRotations.Shift();
}
}
This is not a perfect solution (it is slow and the camera lag is specified in frames, not seconds), but one that is easy to understand and may put you on the right track.
Noob question but how to i import that to my game @Rodin
Your answer
Follow this Question
Related Questions
Camera Movement Smoothing 1 Answer
Particles lag/stutter - choppy movement 3 Answers