- Home /
My animation is overriding my character movement?
Hi, probably a stupid question.
So at the moment I'm just trying to setup a basic human character which I can control, if it moves it changes to the walk animation and if I'm not moving it, it becomes Idle.
I've got a couple of issues:
When I move the player and the animation kicks in it overrides my movement. On the walk animation this means it'll force my character to move forwards even if I'm going in another direction.
It doesn't change animation state straight away, there's a delay between me pressing the button and between when it switches from Idle -> Walk
How do I change the animation to work off Vector3 movement
My code for the controller is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public Rigidbody rb;
public Animator anim;
public float speed;
private void Start(){
rb = GetComponent<Rigidbody> ();
anim = GetComponent<Animator> ();
}
private void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0,moveVertical);
if (moveVertical > 0 || moveHorizontal > 0) {
anim.SetBool ("isWalking",true);
} else {
anim.SetBool ("isWalking",false);
}
rb.AddForce (movement * speed);
}
}
I have an AnimatorController with two states, Idle and Walking. There is a condition on a bool isWalking to switch between them.
The script is attached to my model and I've filled the "anim" parameter with my model animator.
Answer by Glenn-Korver · Oct 21, 2018 at 11:09 PM
Sounds as if you have "applyRootMotion" enabled on your animator, this is enabled by default and will make your gameobject move based on the translation and rotation in the animation, there are reasons on why to use it. But most of the time for games you will likely want it be disabled.
Actually I prefer to have it enabled, and then choose which animations should use root motion, or which should ins$$anonymous$$d bake it into the bones and leave the root alone.
That way, if I want an attack animation to move my character forward slightly, I have that option available. But for jogging animations, I bake any root motion into the bones, so my script is the only one moving the root at that time, based on player input.
Answer by ModLunar · Oct 21, 2018 at 11:37 PM
This isn't an answer to your whole problem, but - I hope this helps :)
For the root motion, I usually keep it checked on in the Animator, but make sure that clips like walking/jogging have it all baked into the bones (so those animations don't affect the root at all -- just the character's bones in the animation). So make sure the three options are checked on for the AnimationClip: "Root Transform Rotation - Bake Into Pose", "Root Transform Position (Y) - Bake Into Pose", and "Root Transform Position (XZ) - Bake Into Pose".
For the transition delay, you should check the transition from Idle -> Walk. Expand "Settings" in the Inspector, and check that "Exit Time" is false. If Exit Time is on, then the transition will have to wait for the current animation state to finish a full cycle before the transition can occur. In this case, you usually wanna start jogging immediately, even if Idle was like 37% of the way done or something. So having Exit Time off will allow it to start the transition in the middle of Idle at any time.
Also, I notice you're using Input in FixedUpdate. You should move any input "getting" into Update, as that's a more reliable time for Unity to ask for it -- FixedUpdate may be off-sync with Update.
The good news is you can still do your thing in FixedUpdate if you create private instance variables in the class to remember what the last Update's values were :). That's actually what I usually do -- it's like the best of both worlds I think because you're getting input each frame properly, and then applying forces based on that, but during the physics update (which happens in fixed time steps). It looks something like this:
public class PlayerController : MonoBehaviour {
public Rigidbody rg;
public Animator anim;
public float speed;
private float moveHorizontal;
private float moveVertical;
public void Start() {
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
public void FixedUpdate() {
Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
rb.AddForce(speed * movement * Time.fixedDeltaTime); //Note: multiply by the time interval to keep your speed dimensionally correct
}
public void Update() {
moveHorizontal = Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
//I'd still update the Animator here, because this is where moveHorizontal and moveVertical actually change
animator.SetBool("isWalking", moveHorizontal > 0 || moveVertical > 0);
}
}