How to rotate spine by the script, while an animation from the animator is playing?
Hi! I want to create third person shooter, but I have got one problem. I want to rotate shooter's spine by script, during an animation from the animator. And while the animation is playing, I can't rotate spine by the script. I don't know how can I do it. I'll be very grateful if someone helps me.
Answer by Arocide · Sep 17, 2017 at 11:42 AM
When you begin using Mecanim for Animations you must play by its rules. Mecanim updates transforms later then Update
and FixedUpdate
messages, so modifying transforms in there that are now under the control of the Animator will just be overridden. So in order to tell Mecanim we want to look at a certain point or procedurally change the rotations of bones we must use Inverse Kinematics (IK). The first step to using IK in Unity is to enable an 'IK Pass' on your Animation layer the documentation here shows you where the tick box is to enable that.
Once you have done that the Animation system will now perform Inverse Kinematics calculations and provides you with a call back to use in your scripts with the MonoBehaviour.OnAnimatorIK message. You may be able to simply use the inbuilt LookAt functionality to get what you are looking for. If so it could be as simple as the following to get what you are looking for:
using UnityEngine;
public class LookAt : MonoBehaviour
{
public Transform lookTarget;
public float lookWeight;
public float bodyWeight;
public float headWeight;
public float eyesWeight;
public float clampWeight;
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
private void OnAnimatorIK(int layerIndex)
{
// Set our Look Weights for this pass
animator.SetLookAtWeight(lookWeight, bodyWeight, headWeight, eyesWeight, clampWeight);
//Now set our position to look at for this pass
animator.SetLookAtPosition(lookTarget.position);
}
}
Where lookTarget
is the GameObject your looking at (The Editor will still allow you to drag and drop GameObjects into a Transform type variable, unless you need the actual game object for a reason why not save a variable). You can find out what the parameters mean by looking at the documentation Animator.SetLookAtWeight and Animator.SetLookAtPosition
However if this does not work for you, you can also look at other possibilities such as Animator.SetBoneLocalRotation (The documentation actually suggests that it can be used in a similar situation to what you are looking for).
A nuclear option does exist if you do not wish to use IK, you can use the LateUpdate
message in your script to override the animation systems transforms for bones, but that is not the best practice I find.
Hopefully this will get you on the right track to getting that character of yours pointing in the right direction! Best of Luck!
Hello! Thanks for the awesome explanation! I didn't know you could simply use .SetLookAtPosition()
to make the character look at a certain position.
I have the following problem: $$anonymous$$y character holds a weapon. When using the above method, it does look at the specific position, however, the weapon doesn't aim at that position, too. How would you affect the weapon to "look" (or more like aim) at the specific position, too?
Your answer
Follow this Question
Related Questions
[HELP PLEASE] How can i check if an animator transition from animation to animation has finished? 3 Answers
Attack Script 2 Answers
NEED HELP ANIMATING PLAYER MOVEMENT 0 Answers
Confused about animations 1 Answer
Multiple Enemies Animator Problem 0 Answers