How to detect if a GameObject is currently rotating?
I'm making a 3d game and I have an AI that looks at the player. The problem is that the AI stays in the idle animation while it is turning to face the player. Is there away to detect when the AI is rotating and then play the turn around animation instead of the idle animation?
How do you rotate this GameObject? Are you using rigidbody.angularVelocity, rigidbody.AddTorque, or are you manually adjusting transform.rotation every frame?
Is body rotating? In the case that you are using the two methods using rigidbody, you can simply read the angularVelocity field of rigidbody to see if the body is rotation or not. If you are using the arguably worse method of manually setting the rotation directly, you'll have to manually make a system that compares the rotation of the body this frame to the previous frames.
Influence animation If animations still work the same as when I last used them, you can make and change parameters that can be used in the Animator component with animator.SetFloat(...) and the alike.
I'm rotating it by the transform. The AI does have a rigidbody attached to it though. Here is the code I used.
Vector3 direction = looktarget.transform.position - transform.position; direction .y = 0.0f; if (direction != Vector3.zero) { transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * velocity); }
Answer by lgarczyn · Jan 15, 2020 at 09:09 AM
You shouldn't use a rigidbody on something moved by transform, and shouldn't use the transform of something with a rigidbody.
The first step would be to fix that, and call rigidbody.MoveRotation in FixedUpdate instead of setting transform.rotation.
Once that is done, simply store the previous rotation before updating it. Then calculate the angle between the new and the old rotation using Quaternion.Angle. If the angle is more than a certain amount you chose, then movement was detected.
The Characters I use are humanoid and I constrained rotation on all axis because I don't want the characters AI rotation to Change if the player bumps into them. Is there away to rotate it with rigidbody while rotation is constrained?
"FreezeRotation" should only prevent the physics engine from rotating the object, $$anonymous$$oveRotation should work perfectly fine, though setting "angular velocity" will likely do nothing.
Your answer
Follow this Question
Related Questions
Idicator arrow flips upside down when player is below certain y axis. SOLVED!!! 0 Answers
Problem with rotating object that has animator attached 1 Answer
Transform.Rotate not working around certain axes 1 Answer
How do you rotate the player and keep them there? 1 Answer
Rotate object in 3d space along one axis in the direction of movement vector. 0 Answers