- Home /
Question by
ZaxOnTheBeach · May 25, 2013 at 12:15 AM ·
camerarotationrotatefixedupdate
Camera 'FixedUpdate' and Rotate function problem
Hey everyone, I have a 3rd person camera script with a rotate function that allows me to rotate around my character on a 90 degree at-a-time basis and stop. However I noticed that the Updates between my character movement and my camera were clashing poorly so I changed the LateUpdate for my camera functions to FixedUpdate which solved that problem. But now when I use my Rotate function in the game when the rotation is complete I noticed the camera wigs out for a split second. (Only when my character is moving! When the character is still this doesn't occur)
Here is the script;
#pragma strict
var rotationSpeed : float = 5.0;
var rotateTime : float = 3.0;
private var rotateDegrees : float = 90.0;
private var rotating : boolean = false;
// Player
var target : Transform;
var distance = 10.0;
var zoomSpeed = 10.0;
public var height = 5.0;
var heightDamping = 2.0;
//Zooming Parimeters
var maxZoomOut = 25.0;
private var maxIn = 5.0;
//UNUSED
private var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
//Controls for zooming the camera
distance = distance -= Input.GetAxis("RightStickY") * Time.deltaTime * zoomSpeed;
distance = Mathf.Clamp(distance,maxIn,maxZoomOut);
if (Input.GetAxis("RightStickX") == 1) {
Rotate(transform,target,Vector3.up,rotateDegrees,rotateTime);
}
if (Input.GetAxis("RightStickX") == -1) {
Rotate(transform,target,-Vector3.up,rotateDegrees,rotateTime);
}
}
function Rotate (thisTransform : Transform, target : Transform, rotateAxis : Vector3, degrees : float, totalTime : float) {
if (rotating) return;
rotating = true;
var startRotation = thisTransform.rotation;
var startPosition = thisTransform.position;
transform.RotateAround(target.position, rotateAxis, degrees);
var endRotation = thisTransform.rotation;
var endPosition = thisTransform.position;
thisTransform.rotation = startRotation;
thisTransform.position = startPosition;
var rate = degrees/totalTime * rotationSpeed;
for (var i = 0.0; i < degrees; i += Time.deltaTime * rate) {
yield;
thisTransform.RotateAround(target.position, rotateAxis, Time.deltaTime * rate);
}
thisTransform.rotation = endRotation;
thisTransform.position = endPosition;
rotating = false;
}
Any help would be greatly appreciated!!
Comment
Your answer
Follow this Question
Related Questions
How to restrict rotation properly 2 Answers
Float the camera back to his position 1 Answer
How to rotate my camera? 2 Answers