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:
var target: Transform;
function Update () {
transform.LookAt(target);
}
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.
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
var target: Transform;
function Update () {
transform.LookAt(target);
}
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 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.
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.
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
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.
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.
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!!
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.
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?