Projectiles either rotate or translate but can't do both

I’ve run into an odd bug with the Glitch Garden game. I’ve followed all of Ben’s code but for some reason Unity is being very stubborn with the projectile animations. They will rotate 360 within the animator, no problem. But when I add the transform.Translate(Vector3.right * speed * Time.deltaTime) code, it doesn’t work as expected. The projectiles instead swing in a big circle. Increasing the projectile speed only increases the radius of the circle. When I turn off the animator component, the transform.Translate command moves the projectiles across the screen from left to right as they normally should. So it seems I can either rotate with the animation component or translate with the script, but not both at the same time.

I have been looking into it for a while but can’t seem to find any solution online or in the course. There is a warning in the animator that says “Root position or rotation are controlled by curves” which leads me to believe that the “Apply Root Motion” checkbox may assist in getting this to work but haven’t had any success yet. If anyone has any tips on how to fix this please let me know!!

  • Brian
1 Like

Hello @TriskelionStudios, how are you?

Is the animation being applied to the parent of the object with the sprite or to the object itself? If you animate the body of the parent object (which have the script for translating it) instead of the child (which is where the sprite is), this kind of behavior can happen.

Take a further look at this class:

Animating Defenders & Projectiles

let me know if you got it working

Hi Joao thanks for looking into this. I checked today and both the Animation and projectile script are on the projectile game object (axe) and not on the body. So I’m not sure why it’s acting this way and why they both work fine individually but not together.

Triskelion,

The animation in the Axe is related to the Body transform or to the axe transform (it should be an animation inside the Axe, but that controls the Body rotation)?

Take a look at 14:40 and further from the class that I linked above.

This kind of bug usually happens when you add a script that changes the transform of an GameObject while it already have an animation that changes it too, that is why you have to add a child and animate separately in order to both animation and the script movement to work

1 Like

Aha that was it! I changed the animation to rotate the body (child) instead of the projectile (parent) and that worked. It’s odd because both the animator and projectile script are components of the projectile (parent) so you would think that you have to animate the projectile parent. But for some reason it doesn’t work that way. Good quirk about Unity that I’ll file away as a mental note for future projects.

Thanks for the tip Joao!

2 Likes

You are welcome @TriskelionStudios!
I’m glad that it was solved!