- Home /
Enemy AI capsule collider tilts when moving
I have set the collider's rotation along all axes to zero. I have constrained the collider's position and rotation along its XYZ axes. I have added mass to its rigidbody. I have checked the the rotation of the game object during animation in Blender (it was upright).
Yet, whenever I hit play, the collider rotates back along the X axis, making the AI appear to be leaning back about 10 degrees. Regardless of the direction he is moving, he looks like he is going to fall backwards.
Any suggestions?
Update: I tried adding this to the Update function.
gameObject.transform.rotation.x = 0;
It had no effect.
collider is a child of the player. Player is probably leaning back through code or animation.
cast a debug ray from player position to player transform up
is the ray angled or straight up on a flat surface?
O$$anonymous$$, the ray drawn in the first frame is straight up. Every subsequent ray is angled back, apparently at the same angle as the collider.
The surface isn't completely flat--it's a terrain--but the difference between the first frame and every subsequent frame is abrupt and significant. I guess that indicates something in the code or animation is the problem, right?
yep as i suspected your player is somehow having what it thinks is up modified. I.E your player is being rotated somehow.
what this tells you is the problem isn't in the collider its in the player.
disable and enable the animation script to see if its animation problems.
O$$anonymous$$, well I disabled the animation script during runtime, but the collider kept its (incorrect) rotation.
well its not animation something is rotating the object. $$anonymous$$aybe use debugging to monitor the Y axis rotation and when it changes
Answer by MP0732 · Nov 24, 2012 at 05:48 AM
OK, I finally figured out what the problem was.
I had set the enemy AI's target position along the Z, X and Y axes. This caused the enemy AI to "tilt" up or down along its own Y axis in order to reach the target.
To fix the tilting, I just zeroed out the Y axis value of the target position. I found the following code on the answer to another question, elsewhere on the forum.
Hat tip, and many, many thanks, to Aldo Netto.
function Update () {
var lookDir = target.position - myTransform.position;
lookDir.y = 0; // zero the height difference
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Animation affects the rotation.. 1 Answer
Lerping smoothly between animation and new position 2 Answers
Animation problem with rotation and position. 0 Answers
Rotation Y axis is bugged. 2 Answers