- Home /
Animation in Unity editor prevents rotation in script
What I'm trying to do
A 2D character with a bazooka attached. Character can move only left and right on the floor. When pressing up or down the bazooka will rotate, kind of like the bazooka in "Worms".
I animate the body parts of the game character when the he walks and would also like to animate the bazooka to move a little when the character is walking. I would prefer to do this visually with a key frame animation, not in code.
My problem
The code for rotating the bazooka works perfectly until I created a key frame animation involving the same bazooka. To confirm I then removed the GameObject from the key frame animation and the rotation starts working again.
Just to be clear, the animation is not running and is not suppose to be running the same time as the rotation is occurring.
Basically it seems I have to choose between the keyframe animation of the bazooka or being able to rotate the bazooka on key up or down at all.
Is there a technique to do this "the right way", without having to make this choice?
Code used for rotation
Not sure its needed but the code below runs in FixedUpdate to rotate the weapon around it's own axis when pressing up or down.
if (Input.GetKey ("up")) {
weapon.transform.Rotate(Vector3.forward);
}
if (Input.GetKey ("down")) {
weapon.transform.Rotate(Vector3.back);
}
I don't know too much about the specific issue, but what you could do is to put the visuals and animation as a child object and then rotate the parent object, then your animations will work in local-space so it seems like he is not rotated (as he isnt, just his parent, which will change how the "world" seems to be rotated) and the animation doens't have to care.
Thanks for the comment! Seems like a clever solution. Not sure I follow you though with adding the visuals and animation as child object. Could you please explain that a little more? I also updated my question to more clearly state my issue.
Answer by g8minhquan · Oct 30, 2014 at 01:24 AM
Create a prefab with this kind of structure:
ParentObject
L AnimObject
Make your script control the parent object, while the Animation control the AnimObject
Thanks! What I did exactly was creating a parent object and control its rotation with my script as you mentioned. I Do my keyframe animation on the child object containing the sprite. It works! One issue was getting the same size of the parent as the child. Solved it by assigning the parent a sprite renderer with the same sprite, then disabled that sprite renderer.
Your answer
Follow this Question
Related Questions
How to rotate an object using animation clip in opposite direction? 1 Answer
Counter is not working correctly with LeanTween 0 Answers
how to rotate 2d obj on android 1 Answer
Character still walking when no key is already pressed 1 Answer
Localscale-flipped 2D character retains original rotation since 5.4 2 Answers