Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by MrTeddyBearr · Oct 20, 2018 at 02:16 PM · animationplayermovement scriptplayer movementanimation controller

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ModLunar · Oct 21, 2018 at 11:22 PM 0
Share

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.

avatar image BuzzyRoboYT · Mar 23 at 02:26 PM 1
Share

Thank you! This helped me out

avatar image
0

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);
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

256 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player won't stop moving while blocking 1 Answer

Non slippery movement 1 Answer

Player Dash doesn't work properly 0 Answers

W key to move doesnt work,Movement Script doesnt do anything when i press w 0 Answers

Make Animation Play After Boolean is Set to True? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges