Unity,Javascript!
so heres the problem… I made a camera script for my racing game so the camera will follow the car normally in 3rd person view, the script also involves when the car speeds up it zooms out a little but i think that works well. also when the car reverses it is supposed to go infront of the car and look at it from reverse (like in gta5). this all works how its supposed to except for one problem. when i first run the scene the camera always starts out infront of the car instead of behind it! when i start moving forward it fixes but why is this problem happening and can I fix it. any help is appreciated thanks! ![]()
#pragma strict
var car : Transform;
var distance : float = 6.4;
var height : float = 1.4;
var rotationDamping : float = 3.0;
var heightDamping : float = 2.0;
var zoomRacio : float = 0.5;
var DefaultFOV : float = 60;
private var rotationVector : Vector3;
function Start () {
}
function LateUpdate () {
var wantedAngle = rotationVector.y;
var wantedHeight = car.position.y + height;
var myAngle = transform.eulerAngles.y;
var myHeight = transform.position.y;
myAngle = Mathf.LerpAngle(myAngle, wantedAngle,rotationDamping*Time.deltaTime);
myHeight = Mathf.Lerp(myHeight,wantedHeight, heightDamping*Time.deltaTime);
var currentRotation = Quaternion.Euler(0,myAngle,0);
transform.position = car.position;
transform.position -= currentRotation*Vector3.forward*distance;
transform.position.y = myHeight;
transform.LookAt(car);
}
function FixedUpdate () {
var localVelocity = car.InverseTransformDirection(car.rigidbody.velocity);
if (localVelocity.z<-0.5) {
rotationVector.y = car.eulerAngles.y + 180;
}
else {
rotationVector.y = car.eulerAngles.y;
}
var acc = car.rigidbody.velocity.magnitude;
camera.fieldOfView = DefaultFOV + acc*zoomRacio;
}
