Search Unity

RigidBody and transform.LookAt(target) Problem?

Discussion in 'Scripting' started by joshuaseaver, Mar 1, 2006.

  1. joshuaseaver

    joshuaseaver

    Joined:
    Oct 26, 2005
    Posts:
    28
    I have a game character that has rigidbody applied to it, and I want to look at and attack one of two players when a condition is met.

    My problem is that when I call transform.LookAt(target) from Update(), the character moves toward the target instead of just looking at it. I've removed all other behaviors except this:

    Code (csharp):
    1.  
    2. var target: Transform;
    3.  
    4. function Update () {
    5.   transform.LookAt(target);
    6. }
    7.  
    Should I be using some other method?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Lookat is only changing rotation.

    You might have another script attached that causes the movement or you might have the pivot point of the rigidbody a bit off so that when you rotate it, it looks like you are translating the actual geometry.
     
  3. joshuaseaver

    joshuaseaver

    Joined:
    Oct 26, 2005
    Posts:
    28
    I wish this were true. I had the same suspicion so I made are really simple scene. So I tried making a simple scene with only a cube, a sphere and a plane. The cube has rigidbody applied and has the script

    Code (csharp):
    1.  
    2. var target: Transform;
    3.  
    4. function Update () {
    5.   transform.LookAt(target);
    6. }
    7.  
    Sphere of course has the Transform target and the stock FPSWalker script on it. No other scripts in the scene. I've attatched a little widget version for you to see the results.

    When you move the sphere around, the cube follows it around physically, it doesn't just rotate. Try it.

    I am running Unity 1.21 and OS X 10.3.9.
     

    Attached Files:

  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I think it is fairly safe to assume that LookAt doesn't move the object, it is a very commonly used function and has been in unity since half a year.

    However if you really think you hit a bug, make a small test project, with only one script which looks at another object. Then send the test project over using Report Bug.app.
     
  5. joshuaseaver

    joshuaseaver

    Joined:
    Oct 26, 2005
    Posts:
    28
    I do think it's a bug. Maybe noone has used this with the looker having rigidbody physics before. I've submitted a file and a bug report as you suggested.
     
  6. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    Thanks for filing the bug, joshua. I ran into this problem tonight while working on a project :)
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I'm not sure it's a bug. Usually we reccomend against manipulating an object's transform directly when it has a rigidbody attached.

    A solution would be to apply a torque to the rigidbody until it's facing the right direction.
    Another solution is to make the object a child of the rigidbody and rotate the child object.

    Joe: Feel free to correct me if this doesn't apply to Transform.LookAt
     
  8. joshuaseaver

    joshuaseaver

    Joined:
    Oct 26, 2005
    Posts:
    28
    I'll try doing what you suggested. Thanks for the clarification.
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    What you are seeing might be related to the
    rigidbody.centerOfMass being different from the origin of the transform.

    transform.LookAt will change the rotation of the transform. The rigidbody however might have a different center of mass. (Unity automatically calculates the center of mass from the colliders)

    So you could also fix it by manually resetting the center of mass.
    And of course freezing rotation.
    Code (csharp):
    1.  
    2. rigidbody.centerOfMass = Vector3.zero;
    3. rigidbody.freezeRotation = true;
    4.  
    Usually if the center of mass is a bit offset it is not a big issue - you can't see it - but possibly your center of mass is way off the transforms position?


    But in some cases it is probably easiest to do what keli said:
    Just rotate a child instead of the rigidbody.
     
  10. joshuaseaver

    joshuaseaver

    Joined:
    Oct 26, 2005
    Posts:
    28
    Joachim,
    Thanks for the second option. I tried this in a test file and it does slow the moving toward the target down, but it still wants to inch toward it every so slightly each Update.

    And come to think of it, in the actual game file, the center of the tiger in may be way off...

    Tried rotating a child. Worked perfectly. Thanks Keli!!
     
  11. summernight2001519

    summernight2001519

    Joined:
    Jul 3, 2018
    Posts:
    2
    This is not a bug. if it look at the target that is higher than it, it rotates upward.
    just because rigidbody is attached, it keeps falling and rotate to the normal rotation so it will move slightly forward,
    cause it keeps doing that so it looks like it's moving.
     
  12. Deleted User

    Deleted User

    Guest

    Did you look at the date of the last post? :)
     
  13. heronww

    heronww

    Joined:
    Sep 8, 2019
    Posts:
    30
    This issue is exactly as described for me even in 2019 and a Google search led me here. Same problem. I guess if it's intended behavior this should be mentioned in the documentation on Rotations interacting with Rigidbody?