Search Unity

transform.LookAt not working

Discussion in 'Scripting' started by ohLogical, Mar 12, 2012.

  1. ohLogical

    ohLogical

    Joined:
    Feb 12, 2012
    Posts:
    98
    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. function Update () {
    5.  
    6. transform.LookAt(target);
    7. }
    The object doesnt turn. Ideas?
     
  2. monster3

    monster3

    Joined:
    Feb 27, 2012
    Posts:
    45
    im pretty sure the only things it could possibly be is you dont have the var target assigned in the inspector, or you havent attached to script to a GameObject. Other than that there is nothing that would stop this from working.

    Oh it could also be I guess if you have an empty scene except for the two objects you are doing this with, it could be that its doing it instantly, so you just don't see it turning. so instead of

    transform.LookAt(target);

    you could do

    transform.LookAt(target * Time.deltaTime * speed);

    and then you set speed to like 10 or something and you can see it rotating.
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Except that's not how LookAt works.
     
  4. monster3

    monster3

    Joined:
    Feb 27, 2012
    Posts:
    45
    I'm pretty sure I've used it like that, but I would like to hear your explanation of how it works.
     
  5. ohLogical

    ohLogical

    Joined:
    Feb 12, 2012
    Posts:
    98
    So ummm......

    anyone know what my problem is?
     
    araz01 likes this.
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    LookAt takes a Transform (or Vector3) and looks at it instantly. Multiplying the target by Time.deltaTime or speed cannot be done to a Transform and does not output what you are hoping for from a Vector3.

    Either something else in your project is also setting the rotation each frame or you are simply not assigning the script correctly.
     
    ianyes123 likes this.
  7. monster3

    monster3

    Joined:
    Feb 27, 2012
    Posts:
    45
    Ah Dman (as you know) you are right. Wow that was stupid of me, I was thinking that if you didn't do what I was doing then it would look instantly, but it DOES look instantly and it only changes smoothly because the object it is looking at is rotating smoothly.

    Haha my bad.
     
  8. ohLogical

    ohLogical

    Joined:
    Feb 12, 2012
    Posts:
    98
  9. Attila7894

    Attila7894

    Joined:
    Mar 11, 2011
    Posts:
    31
    To rotate smoothly you should use Slerp:

    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position), rotationSpeed * Time.deltaTime);
     
  10. candc

    candc

    Joined:
    May 7, 2012
    Posts:
    1
    hello
    I am beginner i have problem like u.
    i flow scripting tutorial from here:
    http://download.unity3d.com/support/documentation/Images/manual/ScriptingTutorial.pdf
    and i have same problem for flowing tutorial in page 4 with "lookat " function .
    my mistake was using point light instead spot light .looks like for point light "lookat" function have no mean.
    be careful use spotlight instead point light .
    ---------
    sorry for my English writing. :mrgreen:
     
  11. araz01

    araz01

    Joined:
    Aug 2, 2016
    Posts:
    53
    odd, this worked and now it just dont... lol... in other words... odd
     
  12. araz01

    araz01

    Joined:
    Aug 2, 2016
    Posts:
    53
    wow code wurks great!
    is it fossible to do it only on one axis? or is that like super Not possilbe..
     
  13. araz01

    araz01

    Joined:
    Aug 2, 2016
    Posts:
    53
    wurks:

    public void NeedAShower() {
    //or update void


    transform.rotation = Quaternion.Slerp(Quaternion.Euler(TargetToLookAt.position.x, -90f,0f), Quaternion.LookRotation(TargetToLookAt.position), 2f * Time.deltaTime);


    //you just need this code in update and a transform target, make public target and add target and use this to aim at the ting

    }

    a little
     
    Last edited: Mar 15, 2020
  14. justresolvejr

    justresolvejr

    Joined:
    Sep 13, 2020
    Posts:
    4
    Dat doesn`t work all it does is make my cube move around like crazy.