- Home /
How to slow down transform.LookAt
Im trying to make the camera move with the mouse pointer in my game using:
public class camera : MonoBehaviour {
public static Vector3 Point;
public float zDistance;
void FixedUpdate(){
var mousePos = Input.mousePosition;
Point=camera.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,zDistance));
transform.LookAt(Point);
}
}v
It works but the screen moves really fast, too fast to be able to play properly. Is there any way to slow it down?
You can save the current rotation as "from", and then use transform.lookat and then save the rotation as "to" and then set the rotation to quaternion.lerp from, to, Time.deltaTime as time and in update ins$$anonymous$$d of fixedupdate
And then you can multiply time if you want it faster or slower like Time.deltaTime*0.5f
Answer by EvilTak · Dec 26, 2014 at 05:35 AM
From my answer to this question: you should use Quaternion.FromToRotation and then use Quaternion.Lerp. Something like this (This code should be in FixedUpdate for smooth results):
Vector3 direction = Point - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);
You can set the speed variable to how slow or fast you want the rotation to be. You can look at the result here (although there the y axis of the object is pointing towards a point).
Answer by mxoconnell · Sep 02, 2019 at 01:19 AM
Vector3 relativePos = focus.position - transform.position;
Quaternion toRotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Lerp( transform.rotation, toRotation, 1 * Time.deltaTime );
So Lerp and Slep work fine to get good ease-out animation on rotation, but for good naturalistic movement you need ease-in, as well. $$anonymous$$athf.SmoothStep can help on anything that accepts float values, but Quaternions are trickier to implement that way.
I'm kind of fishing here, but do you know of a good way to ease rotation on both sides of the animation?
This kind of misuse of lerp with deltaTime is like a disease. People keep spreading it, newcomers keep catching it.
Answer by Cheeseman · Jun 03, 2018 at 09:11 PM
I know this post is super old, but I wanted to amend @Evil-Tak 's post:
If Quaternion.FromToRotation()
doesn't work, try using Quaternion.LookRotation()
in it's place (slightly different params).
Thank you. I was wondering why @Evil-Tak 's method wasn't working. You solved my problem.
Answer by CodeMonkeyYT · May 03 at 09:12 AM
Everyone else is already correctly mentioning how to modify transform.rotation, but personally I find Quaternions to be really confusing so my preferred method for rotating is by directly modifying transform.forward
You just calculate the direction vector with (targetPosition - transform.position).normalized
You can combine it with Vector3.Lerp to get a nice smooth rotation https://unitycodemonkey.com/video.php?v=jAN2IoWdPzM