- Home /
Controlling body parts during animations with scripts
I'm making an archer in a 2D game. I've made an arrow drawing animation which leaves the player aiming in a horizontal direction. I now want the player to move the aiming direction by moving the mouse around. I know how to calculate the aiming angle (from the center of the player).
So the thing I'd like to know is how do I rotate my body parts around to match the direction I am aiming in? This is what I've tried:
I've made a new animation, "hold", which is automatically transitioned to after the arrow draw animation. It is just one frame, and it is a copy of the last frame of the drawing animation. As long as the player holds the mouse, we are in the hold animation. Now I wanted to rotate the right bracer and left arm via scripting, but alas this did not work:
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("hold")) {
rightBracer.rotation = Quaternion.Euler(0,0, aimAngle + 474.3156f); // 474. is the rotation when the player is aiming right
leftArm.rotation = Quaternion.Euler(0,0, aimAngle + 69.52766f);
}
I've figured out that the reason is probably that the animation clip "hold" is overwriting these changes.
So there's two possible solutions I can think of but I do not know where to start for both of them:
1) There could be a way to directly overwrite the animation or atleast make sure the animation does not overwrite my scripting values.
2) I should delete the hold animation, so my player is no longer in an animation state. But how does this work? Should I make a transition to the "Exit" state in my animator controller? How do I stay in (a modified version of) the last frame of the drawing animation? And how do I go back to animations?
How would you fix this?
Answer by Nischo · Jul 09, 2015 at 09:51 AM
I would try to fix this with a blend tree. So have 2 extreme aiming animations for up and down, and use the actual angle to blend between these two values. If necessary you can do that with a 2D blend tree and 4 positions for up/down/left/right aiming.
Thank you! I totally missed the blend part of blend trees :O So I thought Id get a discrete set of positions, but obviously there is some blending like the name suggests. Problem solved!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I rotate my character smoothly? 1 Answer
Distribute terrain in zones 3 Answers
Navigating the 3D platform 0 Answers
Rotate Animation In-Game? 2 Answers