I have an object, that i rotate through a script. And i have an animation, that also rotates this object. But when i make the animation, even if the script is not going it prevents the script from rotating this object. How do i make it so that when the animation is NOT playing it allows the script to rotate the object.
Just set applyRootMotion = true; when you don’t need the animation to take on your own movement.
I’m a little confused, do you want to check if the Animation is not playing? To do so, you must check the parameters you plug into Mecanim (assuming you’re not using Legacy animation). For example if you rotate the object based off “parameter Rotate > 5” then just check if that parameter is not greater than 5.
However, I think what you really want is to override the rotation of the animation and rotate from the script instead. To do so, simply call your rotate function in LateUpdate() instead of Fixed or regular Update(). LateUpdate() is called after Update(), where all animation happens.
Ok, I know this is an old post but I had a similar issue in 2019. I am making a rails shooter type game (think starfox 64) and I needed the enemies to be on their own timeline or rail animation (Which moves their physical position in the scene but also locks their rotation to whatever rotation is recorded in the animation) while still being able to move the enemie’s game object’s local rotation to face the player so they could shoot at him randomly.
After several hours of research, I got it. - this post being the most relevant:
this bit of code I got from another topic in these forums, and it makes the enemy face the player:
public class LookAtPlayer : MonoBehaviour
{
[SerializeField] Transform target;
void Update()
{
if (target != null)
{
transform.LookAt(target);
}
}
}
But this is not enough in my situation since the rotation of the enemy is locked by the animation of the rail. Then I found this:
A rotation constraints component that you can add to a game object. It allows you to constrain the object’s rotation to a rotation of your choosing OR to mimic the rotation of another game object that you set under “sources”. So:
Step 1 - put all the enemies you want to behave this way under a parent empty game object (name it anything that fits what your working on).
Step 2 - make an additional child to this parent object and call it “Look At Player” (name optional) make sure the transform of the “Look At Player” game object is close to the enemies or enemy that you are trying to control. (so it faces the right way)
Step 3 - Make a script with the LookAtPlayer code I showed above and save it.
Step 4 - Attach the LookAtPlayer script to the LookAtPlayer game object and drag your player’s prefab into the “target” field of the LookAtPlayer script.
Step 5 - add a rotation constraints component to all the enemies you want to control this way, click the “Zero” button, add a source to the source list and drag the LookAtPlayer game object to make it the source of the enemies rotation.
You are done, the result should be:
- your animation plays like you wanted it to
- your enemy is mimicking the rotation of the LookAtPlayer game game object (which is not restricte by an animation)
- the LookAtPlayer game object has a script that always looks at the player.
- Thus your enemies now will always look at the player.
Sort of a “monkey see monkey do” result. Don’t know if this is an answer or a work-around but it works. Hope this helps others with similar situations.