- Home /
How do I rotate an object towards a Vector3 point?
I'd like to rotate an object towards a Vector3 point without using LookAt. LookAt seems to instantly lock your view on the object. Does anyone know how to do this? I'm most familiar with C#, so if you could give an example using C# that would be great!
Answer by asafsitner · May 18, 2012 at 10:56 AM
If I understand you correctly you want to rotate an object to 'look at' another object over time. One way to do it is with Quaternion.LookRotation and Quaternion.Slerp:
using UnityEngine;
public class SlerpToLookAt: MonoBehaviour
{
//values that will be set in the Inspector
public Transform Target;
public float RotationSpeed;
//values for internal use
private Quaternion _lookRotation;
private Vector3 _direction;
// Update is called once per frame
void Update()
{
//find the vector pointing from our position to the target
_direction = (Target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
}
}
Also, transform.rotation = Quaternion.RotateTowards(transform.rotation, _lookRotation, maxDelta)
for when you don't know the percentage but rather the increment
Answer by Phoera · Jul 03, 2015 at 07:33 AM
or simply
transform.LookAt(Target)
this is ok, but really bad idea to use for an actual game as it locks to the target, most people want control over their Ai to give them more realistic movement or custom movement more to say. unfortunatly unity doesnt have any other alternative way to adjust the LookAt function in terms of rotation speed.
A simple read of the original question would lead you to the fact that transform.LookAt(target)
did not have the desired effect.
Answer by yoyo696 · Oct 14, 2016 at 03:37 PM
Can you make it to a 2d game please???
Yes I tried but the 2d gameobject rotates I think in a 3D way till I can't see it anymore but when I convert the from a 2d to a 3d scene in the editor It's there and I can see it like a quad.Thanks For Help.
Add a
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z)
It will fix this
Due to Unity's power of adapting both 3D and 2D planes with the same library methods, it sometimes doesn't really makes sense to translate the same methods directly from 3D to 2D. If you're making 2D game and just simply want to make something to look at a certain vector in world space (for example, turns a top down sprite towards mouse position), there's a lot easier way to do it. Just assign whatever your directional vector to the transform.right
, but make sure to cast the direction to Vector2 before you do so (only to avoid the z axis confusions, nothing special). Works like magic. Here's the code example.
Vector2 dir = this.mouseTrack.position - this.transform.position;
this.transform.right = dir;
mouseTrack
is a transform I've attached to the input mouse position, which isn't really important here. You can get the dir
vector anyway you like. Hope that helps.
This basically means that you're treating your transform.right
as a forward directional guide. Forget the Quaternion and rotation methods, forget the angle calculations, just pull the axis around to turn something to a certain direction.
On a side note though, I think Unity needs some simpler separate ways of manipulating transform only on the 2D axes.
very clever. this is a great answer. I'd subscribe to your 2d tips! Do you have more?
Thankyou! I ended up having to adjust the rotation by -90 degrees since my image was upward facing. But nonetheless great solution
Your answer

Follow this Question
Related Questions
How do you perform multiple rotations on the same object? - (in a single frame) 0 Answers
Rotating a direction Vector3 by Quaternion 2 Answers
Rotate Vector3 array around a point 1 Answer
Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer
Play incorrect animations when is looking in other direction 0 Answers