As is, this code will cause it to snap to the angle instead of lerp towards it. Smooth is set to 5 currently in the demo and it seems that the higher I put that number, the more angle I will get. Please play the demo to fully see the issue. Demo
I have tried Mathf.Lerp. Also, without the smooth value it will only ever so slightly budge. As if it’s only doing it on one frame but I have checked and it’s constantly being accessed.
if (ship.position.x > 5) {
angle = Mathf.LerpAngle(camera.rotation.y, 40f, Time.deltaTime * smooth);
camera.eulerAngles = new Vector3(10, angle, 0);
}
else if (ship.position.x < 2) {
angle = Mathf.LerpAngle(camera.rotation.y, -40f, Time.deltaTime * smooth);
camera.eulerAngles = new Vector3(10, angle, 0);
}
else {
angle = Mathf.LerpAngle(camera.rotation.y, 0f, Time.deltaTime * smooth);
camera.eulerAngles = new Vector3(10, angle, 0);
}
‘rotation’ is not what you thing it is. Please read my comments and suggestions about Quaternions and eulerAngles in my answer to this question:
http://answers.unity3d.com/questions/415717/rotate-an-element-with-exactly-value.html
Even if you change the code to use eulerAngles instead of rotation, you cannot depend on any specific eulerAngle representation. You could assign (180,0,0) to eulerAngles and immediately read it back and get (0,180,180) (the same physical rotation).
This is a common mistake in using Lerp. Lerp functions actually do not smooth the value towards the target automatically. they work like a slider, you have to manually change the slider value yourself and you can do it in your code.
the slider is the t value which is a float and should be between 0 and 1.
here is how Lerp functions:
LerpAngle (a : float, b : float, t : float)
it looks at t vlaue and gives you a number between “a” and “b”. if “t” is 0 or less it returns “a”, and if “t” is 1 or more, it returns “b” and if “t” is between 0 and 1 it gives you a number between “a” and “b” and uses “t” as the ratio.
what you did in your code is using Time.deltaTime * smooth
as t value which if used in update function, it is pretty much a constant. so Lerp will always return same number.
if you want your angle to change you have to give “t” parameter a value which increases/decreases every frame, so you have to use Time.time
instead and have a temporary variable as “now”. here is a simple implementation:
private var now: float;
private var letMove: boolean = flase;
function myMove(){ // call myMove on this gameObject
// and it will start movement to x=10
now = Time.time;
letMove = true;
}
function Update(){
if (letMove){
transform.position.x = Mathf.Lerp(0,10,(Time.Time - now));
}
}