var targetObj : GameObject;
var speed : int = 5;
function Update(){
var targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
Do you have a smart way of detecting if the camera finished the movement? So that I can take away control of the camera while it’s doing the smooth look at but give it back once it’s done looking at that certain object.
I think this is a great solution, I’m agree with Ineno; i need to know when the rotation finish to stop this automatic rotation and make the manual rotation again.
you can use a raycast to determine if you are pointing to the desired object and so to know if the rotation is finished (maybe wait 0.2s (depends of the rotation speed) to let it looks at the center of the object)
I think I am pretty late maybe, but instead of raycast, u can turn this into a coroutine and then know if it has finished, something like this
IEnumerator RotateTransform(float dur)
float t = 0f;
while(t < dur){
t += Time.deltaTime;
float factor = t / dur;//use this for lerping
yield return null;//basically wait for next frame
}
print("Rotation Finished");
}
If I’m not mistaken (and I could be!!), this will never actually resolve. The 3rd parameter should be between 0 and 1, where 0 will return transform.rotation and 1 will return targetRotation.
Anyone have a clever way of returning the LookAt control smoothly back to an animation clip that was running? I’ve got a scenario where an animated rigged object needs to have the LookAt relinquished without snapping back to the looping animation that was controlling the bone to start. So far, I don’t see a way of reading the clip’s key values at runtime. Maybe a potential way of creating another state to transition to, but the SetLookAtPosition is only for humanoid rigs afaik. Any ideas? It would great if the LookAt function had a weight to it, like the SetLookAtPosition has available.
That is not a linear movement tho. It’ll get slower and slower at the end because it is always lerping (slerping in this case) with percentage and it is modifying the start position constantly. Also it is better to do these stuff in FixedUpdate.
Stop using Lerps (or Slerps, or whatever) with speed * Time.deltaTime or whatever, for movement or rotation. It is a horrible thing and will behave differently depending on framerate, don’t do it.
This isn’t the the way to use interpolation, no matter what Unity tells you, they also don’t know any better.
It’s a note, on a really old thread with really old advice that has people calling the original solution genius.
The note is : “this solution is bad, please look elsewhere” and it’s supposed to urge people to research this further (and stay away from Unity’s tutorial’s, since they do this themselves and are a big reason why people keep misusing lerps).
I think that has value of being in this thread.
You obviously don’t, demonstrated by your misquoting me, but I don’t think that’s my fault.