x


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!

more ▼

asked May 18 '12 at 08:46 AM

rdshaner gravatar image

rdshaner
34 2 4 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
    }
}
more ▼

answered May 18 '12 at 10:56 AM

asafsitner gravatar image

asafsitner
2.4k 2 8 19

Thanks I got it!

May 18 '12 at 02:48 PM rdshaner

Thanks good response

Jul 14 '12 at 05:57 PM WhiteSkull

This was exactly what I was looking for. Thanks!

Dec 31 '12 at 03:13 PM Double_D

Thanks! Simple but extremely useful

Jan 20 at 04:25 PM Armand

Awesome!!

Mar 13 at 12:05 PM Creator347
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2163
x575

asked: May 18 '12 at 08:46 AM

Seen: 3598 times

Last Updated: Mar 13 at 12:05 PM