# Smooth look at

**URL:** https://discussions.unity.com/t/smooth-look-at/394528
**Category:** Localization
**Tags:** Scripting
**Created:** [July 7, 2009, 9:01pm UTC](https://discussions.unity.com/t/smooth-look-at/394528 "2009-07-07T21:01:46Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![radiolobito](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/radiolobito/32/340441_2.png) [@radiolobito](https://discussions.unity.com/u/radiolobito)
#### Post date: [July 7, 2009, 9:01pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/1 "2009-07-07T21:01:46Z")

</div>

How can I make a smooth rotate to object?

this is the code that i’m using

```auto
var localizeobject : Transform; 
var localize = false;

function Update () {
if (localize == true){
transform.LookAt(localizeobject);
localize = false;
}
else
{
}
}

function OnGUI () {
	if (GUI.Button (Rect (10,100,150,100), "Localize Object")) {
		localize = true;
	}
}

```

This causes a instant rotation to the object… how can i make this rotation smooth?

---

<div class="post-metadata">

### Author: ![jaxas](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jaxas/32/532561_2.png) [@jaxas](https://discussions.unity.com/u/jaxas)
#### Post date: [July 7, 2009, 10:26pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/2 "2009-07-07T22:26:45Z")

</div>

try this:

```auto
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);
}

```

😉

---

<div class="post-metadata">

### Author: ![anon\_81963393](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@anon\_81963393](https://discussions.unity.com/u/anon_81963393)
#### Post date: [July 7, 2009, 11:07pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/3 "2009-07-07T23:07:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![radiolobito](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/radiolobito/32/340441_2.png) [@radiolobito](https://discussions.unity.com/u/radiolobito)
#### Post date: [July 8, 2009, 2:13am UTC](https://discussions.unity.com/t/smooth-look-at/394528/4 "2009-07-08T02:13:29Z")

</div>

It’s workin great!

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.

---

<div class="post-metadata">

### Author: ![ieselisra](https://avatars.discourse-cdn.com/v4/letter/i/54ee81/32.png) [@ieselisra](https://discussions.unity.com/u/ieselisra)
#### Post date: [March 11, 2017, 11:57pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/5 "2017-03-11T23:57:25Z")

</div>

> [@jaxas](#):
>
> try this:
> 
> ```auto
> 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);
> }
> 
> ```
> 
> 😉

Works Like a Charm (Unity 5.5.2)

---

<div class="post-metadata">

### Author: ![WalterEspinar](https://avatars.discourse-cdn.com/v4/letter/w/7ea924/32.png) [@WalterEspinar](https://discussions.unity.com/u/WalterEspinar)
#### Post date: [August 31, 2018, 12:57pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/6 "2018-08-31T12:57:28Z")

</div>

- 

```csharp

```

public GameObject targetObj;  
public int = 5;

void Update()  
{  
Quaternion targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);

// Smoothly rotate towards the target point.  
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed \* Time.deltaTime);  
}

```auto

```

---

<div class="post-metadata">

### Author: ![riboslem\_unity](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@riboslem\_unity](https://discussions.unity.com/u/riboslem_unity)
#### Post date: [October 25, 2018, 11:47am UTC](https://discussions.unity.com/t/smooth-look-at/394528/7 "2018-10-25T11:47:22Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![b\_v](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@b\_v](https://discussions.unity.com/u/b_v)
#### Post date: [November 19, 2018, 3:26am UTC](https://discussions.unity.com/t/smooth-look-at/394528/8 "2018-11-19T03:26:20Z")

</div>

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

```csharp
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");
}

```

---

<div class="post-metadata">

### Author: ![DranrebKing](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@DranrebKing](https://discussions.unity.com/u/DranrebKing)
#### Post date: [June 9, 2019, 5:58pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/9 "2019-06-09T17:58:11Z")

</div>

> [@jaxas](#):
>
> try this:
> 
> ```auto
> 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);
> }
> 
> ```
> 
> 😉

This still works! Thanks a lot!

---

<div class="post-metadata">

### Author: ![silvabrid\_unity](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@silvabrid\_unity](https://discussions.unity.com/u/silvabrid_unity)
#### Post date: [November 14, 2019, 6:30pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/10 "2019-11-14T18:30:28Z")

</div>

what can i use to limit rotation on the smooth look at script??

---

<div class="post-metadata">

### Author: ![infinitypbr](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/infinitypbr/32/911828_2.png) [@infinitypbr](https://discussions.unity.com/u/infinitypbr)
#### Post date: [May 6, 2020, 10:15pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/11 "2020-05-06T22:15:54Z")

</div>

I think the answer provided in the 2nd post is not correct.

```csharp
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);

```

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.

---

<div class="post-metadata">

### Author: ![Arcadian-Unity](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Arcadian-Unity](https://discussions.unity.com/u/Arcadian-Unity)
#### Post date: [October 13, 2020, 6:57am UTC](https://discussions.unity.com/t/smooth-look-at/394528/12 "2020-10-13T06:57:11Z")

</div>

> [@jaxas](#):
>
> try this:
> 
> ```auto
> 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);
> }
> 
> ```
> 
> 😉

This worked well for me  
Thanks for sharing

---

<div class="post-metadata">

### Author: ![yonatanab1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/yonatanab1/32/984985_2.png) [@yonatanab1](https://discussions.unity.com/u/yonatanab1)
#### Post date: [August 12, 2021, 5:56pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/13 "2021-08-12T17:56:18Z")

</div>

> [@jaxas](#):
>
> try this:
> 
> ```auto
> 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);
> }
> 
> ```
> 
> 😉

2021 and still relevant! thank you!

---

<div class="post-metadata">

### Author: ![StuGrimson](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@StuGrimson](https://discussions.unity.com/u/StuGrimson)
#### Post date: [November 10, 2021, 2:16am UTC](https://discussions.unity.com/t/smooth-look-at/394528/14 "2021-11-10T02:16:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jandru1](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@Jandru1](https://discussions.unity.com/u/Jandru1)
#### Post date: [December 1, 2021, 11:13am UTC](https://discussions.unity.com/t/smooth-look-at/394528/15 "2021-12-01T11:13:22Z")

</div>

> [@jaxas](#):
>
> try this:
> 
> ```auto
> 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);
> }
> 
> ```
> 
> 😉

12 years later you are still a genious

---

<div class="post-metadata">

### Author: ![razzraziel](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/razzraziel/32/892873_2.png) [@razzraziel](https://discussions.unity.com/u/razzraziel)
#### Post date: [December 1, 2021, 11:58am UTC](https://discussions.unity.com/t/smooth-look-at/394528/16 "2021-12-01T11:58:02Z")

</div>

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.

You can either use a temporary rotation variable:

```csharp
        public Transform target;
        public float speed;

        private Quaternion targetRotation;
        private Quaternion startRotation;
        private bool cached;

        private void CacheStartOnceBefore()
        {
            if (cached) return;

            targetRotation = Quaternion.LookRotation(target.position - transform.position);
            startRotation = transform.rotation;
            cached = true;
        }

        private void FixedUpdate()
        {
            CacheStartOnceBefore();

            transform.rotation = Quaternion.Slerp(startRotation, targetRotation, speed * Time.fixedTime);
        }

```

Or you can use RotateTowards:

```csharp
       Vector3 targetDir = target.position - transform.position;
       targetDir.y = 0.0f;
       transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(targetDir), Time.deltaTime* speed );

```

---

<div class="post-metadata">

### Author: ![AcidArrow](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/acidarrow/32/84064_2.png) [@AcidArrow](https://discussions.unity.com/u/AcidArrow)
#### Post date: [December 1, 2021, 12:01pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/17 "2021-12-01T12:01:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![razzraziel](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/razzraziel/32/892873_2.png) [@razzraziel](https://discussions.unity.com/u/razzraziel)
#### Post date: [December 1, 2021, 2:03pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/18 "2021-12-01T14:03:46Z")

</div>

> [@AcidArrow](#):
>
> stop using that  
> it is horrible  
> unity also don’t know any better  
> bye

how is that post even supposed to help?

---

<div class="post-metadata">

### Author: ![AcidArrow](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/acidarrow/32/84064_2.png) [@AcidArrow](https://discussions.unity.com/u/AcidArrow)
#### Post date: [December 1, 2021, 2:11pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/19 "2021-12-01T14:11:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![razzraziel](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/razzraziel/32/892873_2.png) [@razzraziel](https://discussions.unity.com/u/razzraziel)
#### Post date: [December 1, 2021, 2:25pm UTC](https://discussions.unity.com/t/smooth-look-at/394528/20 "2021-12-01T14:25:03Z")

</div>

It has zero value.

You’re not pointing to anything. What is the alternative?  
[https://www.google.com/search?q=unity%20lerp%20alternative](https://www.google.com/search?q=unity%20lerp%20alternative)

These guys thanking for an example use for Slerp. A basic Unity method openly available in docs. Think about that.

I didn’t misquoted you, that is exactly what it is.

[Next page](https://discussions.unity.com/t/smooth-look-at/394528.md?page=2)
