Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Condore · Oct 23, 2012 at 06:49 AM · movementphysicscharactervelocitydetection

Detecting when my character is not moving?

Hey guys,

Another question from me...

So I'm trying to detect when my character is not moving. I'm currently propelling my character through my game world using Velocity on a Rigidbody. What I want to do, is detect when my character comes to a halt. So that I can call my Idle() animation because, currently it is being detect, just when the player is not providing any input, within update() and it is currently over-riding all of my other animations that I intend to play. Making it impossible to play any other animations.

I have separated the animations I have with layering. But it doesn't seem to work if I just set the idle to a lower layer.

Any suggestions would be appreciated. Is there any way that I can fix this?

Here's the code within my animator script:

 using UnityEngine;
 using System.Collections;
 
 public class TP_Animator : MonoBehaviour
 {
     public enum Direction
     {
         Forward,
         RightBackward,
         RightForward,
         Backward,
         LeftBackward,
         LeftForward,
         Right,
         Left,
         Stationary,
         LockI,
         LockL,
         LockR,
         LockF,
         LockB    
     }
     
     public enum CharacterState
     {
         Idle,
         Walk,
         Run,
         Strafe,
         StartCombo,
         Combo1,
         Combo2,
         Dash,
         Damage,
         LockIdle,
         LockRight,
         LockLeft,
         LockForward,
         LockBackward,
         DashForward,
         EnemyIdle,
         EnemyAttack,
         EnemyRun
     }
     
     public ThirdPersonController Controller;
     public static TP_Animator Instance;
     Targetting targetting;
     
     public Direction MoveDirection { get; set; }
 
     public CharacterState State { get; set; }
     
     
     void Awake ()
     {
         Instance = this;
         
         //Attack animations are on different layer's for prioirity within the combo.
         animation ["dash"].layer = 7;
 
         if (animation ["attack1"]) animation ["attack1"].layer = 2;
         if (animation ["attack2"]) animation ["attack2"].layer = 2;
         if (animation ["attack3"]) animation ["attack3"].layer = 2;
         
         if (animation ["Cattack1"]) animation ["Cattack1"].layer = 3;
         if (animation ["Cattack2"]) animation ["Cattack2"].layer = 3;
         if (animation ["Cattack3"]) animation ["Cattack3"].layer = 3;
 
         animation ["dashright"].layer = 4;
         
         animation ["dashleft"].layer = 4;        
 
         animation ["dashback"].layer = 4;
         
         animation ["dashforward"].layer = 4;
         
         animation ["dash"].layer = 7;
         animation ["idle"].layer = 1;
         
         animation.SyncLayer(2);
         animation.SyncLayer(3);
         animation.SyncLayer(4);
     }
     
     void Start ()
     {
         //bBlackArmor = true;
         targetting = GetComponent<Targetting>();
     }
     
     void Update ()
     {
         DetermineCurrentMoveDirection ();
         DetermineCurrentState ();
         ProcessCurrentState ();
     }
     
     public void DetermineCurrentMoveDirection ()// this functions determines which direction the character is moving an plays animation
     {
         
         if (Input.GetAxis ("LockOn") > 0.5f ) {
             
             if (Controller.movement1.x > 0) {
                 //moving Right    
                 if (Controller.movement1.x > 0) {
                     MoveDirection = Direction.LockR;
                 } else if (Controller.movement1.x < 0) {
                     MoveDirection = Direction.LockR;
                 } else {
                     MoveDirection = Direction.LockR;
                 }
                     
             } else if (Controller.movement1.x < 0) {
                 //Move Left
                 if (Controller.movement1.x > -0) {
                     MoveDirection = Direction.LockL;
                 } else if (Controller.movement1.x < -0) {
                     MoveDirection = Direction.LockL;
                 } else {
                     MoveDirection = Direction.LockL;
                 }
             } else {
                 if (Controller.movement1.z > 0) {
                     MoveDirection = Direction.LockF;
                 } else if (Controller.movement1.z < 0) {
                     MoveDirection = Direction.LockB;
                 } else {
                     MoveDirection = Direction.LockI;
                 }
             }
         } else {
             
             if (Controller.movement1.magnitude > 0) {
                 // moving Right    
                 if (Controller.movement1.magnitude > 0) {
                     MoveDirection = Direction.RightForward;
                 } else if (Controller.movement1.magnitude < 0) {
                     MoveDirection = Direction.RightBackward;
                 } else {
                     MoveDirection = Direction.Right;
                 }
                 
             } else if (Controller.movement1.magnitude < 0) {
                 //Move Left
                 if (Controller.movement1.magnitude > -0) {
                     MoveDirection = Direction.LeftBackward;
                 } else if (Controller.movement1.magnitude < -0) {
                     MoveDirection = Direction.LeftForward;    
                 } else {
                     MoveDirection = Direction.Left;    
                 }
             } else {
                 if (Controller.movement1.magnitude > 0) {
                     MoveDirection = Direction.Forward;
                 } else if (Controller.movement1.magnitude < 0) {
                     MoveDirection = Direction.Backward;    
                 } else
                     MoveDirection = Direction.Stationary;    
             }
         }
         
     }
     
     void DetermineCurrentState ()// State tree, Checking to see if we're doing anything other than moving
     {
         switch (MoveDirection) {
         //Normal Character States
         case Direction.Stationary:
              State = CharacterState.Idle;
              break;
                 
         case Direction.Forward:
         case Direction.Right:
         case Direction.RightForward:
         case Direction.RightBackward:
             if (Controller.isDodging)
                 State = CharacterState.Dash;
             else if (Controller.movement1.magnitude * Controller.speed < Controller.speed * 0.7f)
                 State = CharacterState.Walk;
             else                    
                 State = CharacterState.Run;
             break;
             
         case Direction.Backward:
         case Direction.Left:
         case Direction.LeftForward:
         case Direction.LeftBackward:
             if (Controller.isDodging)
                 State = CharacterState.Dash;
             else if (-Controller.movement1.magnitude * Controller.speed < Controller.speed * 0.7f)
                 State = CharacterState.Walk;
             else                    
                 State = CharacterState.Run;
             break;
                 
         //Lock on States
         case Direction.LockI:
             State = CharacterState.LockIdle;
             break;
         
         case Direction.LockF:
             if (Controller.movement1.magnitude > 0 && Input.GetButtonDown ("Fire1")) {
                 animation.CrossFade ("dashleft");
             } else
                 State = CharacterState.LockForward;
             break;
                     
         case Direction.LockL:
             if (Controller.movement1.x < 0 && Input.GetButtonDown ("Fire1")) {    
                         
                 animation.CrossFade ("dashback");
             } else
                 State = CharacterState.LockLeft;
             break;
             
         case Direction.LockR:
             if (Controller.movement1.x > 0 && Input.GetButtonDown ("Fire1")) {
                         
                 animation.CrossFade ("dashforward");
             } else
                 State = CharacterState.LockRight;
             break;
             
         case Direction.LockB:
             if (-Controller.movement1.magnitude < 0 && Input.GetButtonDown ("Fire1")) {
                         
                 animation.CrossFade ("dashright");
             } else
                 State = CharacterState.LockBackward;
             break;
         }
     }
     
 
 
     
 #region Character State Methods
     public void Idle ()
     {
         State = CharacterState.Idle;
          // animation.CrossFade ("idle");
     }
 
     public void Walk ()
     {
         State = CharacterState.Walk;
         animation.CrossFade ("walk");
     }
 
     public void Run ()
     {
         State = CharacterState.Run;
         animation.CrossFade ("run");
     }
     
     void LockIdle ()
     {
         State = CharacterState.LockIdle;
 
         animation.CrossFade ("lockidle");
     }
     
     void LockForward ()
     {
         State = CharacterState.LockForward;
     
         animation.CrossFade ("lockforward");
     }
     
     void LockBackward ()
     {
         State = CharacterState.LockBackward;
         
         animation.CrossFade ("lockback");
     }
     
     void LockLeft ()
     {
         State = CharacterState.LockLeft;
         
         animation.CrossFade ("lockleft");
     }
     
     void LockRight ()
     {
         State = CharacterState.LockRight;
 
         animation.CrossFade ("lockright");
     }
     
 
     public void Dash ()
     {
         State = CharacterState.Dash;
         animation.CrossFade("dash");
     }
     
     public void Damage()
     {
         State = CharacterState.Damage;
         animation.CrossFade("damageforward");
     }
     #endregion
     
     #region Start Action Methods Katana
     public void Attack ()
     {
     
         State = CharacterState.StartCombo;
 //        animation.CrossFadeQueued("attack", 0.1f, QueueMode.PlayNow);
         animation.Play("attack");
         
     }
     
     public void Attack1 ()
     {        
         
         State = CharacterState.Combo1;
         animation.CrossFadeQueued ("attack1", 0.1f, QueueMode.CompleteOthers);
         
     }
     
     public void Attack2 ()
     {    
         
         State = CharacterState.Combo2;
         animation.CrossFadeQueued ("attack2", 0.1f, QueueMode.CompleteOthers);
     
     }
         
     #endregion
     
     
     
     #region Start Action Methods Claymore
     public void AttackClaymore()
     {
     
         State = CharacterState.StartCombo;
         animation.CrossFade ("cattack");
         
     }
     
     public void AttackClaymore1()
     {        
         
         State = CharacterState.Combo1;
         animation.CrossFade ("cattack1");
         
     }
     
     public void AttackClaymore2()
     {    
         
         State = CharacterState.Combo2;
         animation.CrossFade("cattack2");
     }
     
         
     #endregion
     public void ProcessCurrentState () //Constantly Porcessing the current state of the character
     {
         switch (State) {
         case CharacterState.Idle:
             Idle ();
             break;
             
         case CharacterState.Walk:
             Walk ();
             break;
             
         case CharacterState.Run:
             Run ();
             break;
             
         case CharacterState.LockForward:
             LockForward ();
             break;
             
         case CharacterState.LockBackward:
             LockBackward ();
             break;
             
         case CharacterState.LockLeft:
             LockLeft ();
             break;
             
         case CharacterState.LockRight:
             LockRight ();
             break;
             
         case CharacterState.LockIdle:
             LockIdle ();
             break;
         
         case CharacterState.Dash:
             Dash ();
             break;
             
         case CharacterState.Damage:
             Damage();
             break;
         
         
         }
     
         
         
     }
 
 }

Here's the part of my ThirdPersonController.cs that handles my characters movement.

     void FixedUpdate ()
     // Handle movement here since physics will only be calculated in fixed frames anyway
     {
     
         if(isAttacking == false)
         {
         
             float appliedSpeed = walking ? speed / walkSpeedDownscale : speed;
             
                 
             if(Input.GetButtonDown("LockOn"))
             {
                 appliedSpeed = walking ? strafeSpeed / walkSpeedDownscale : strafeSpeed;
             }
             else
             {
                 appliedSpeed = walking ? runSpeed / walkSpeedDownscale : runSpeed;
             }
         
             if (isDodging)
                 appliedSpeed = dodgeSpeed;
         
             Vector3 relativeMovement = movement1;
 
             if(targetting.selectedTarget != null)
                 relativeMovement = rigidbody.transform.TransformDirection(relativeMovement);
             else
                 relativeMovement = Camera.main.transform.TransformDirection(relativeMovement);
             
             relativeMovement.y = 0f;
             
             rigidbody.AddForce (relativeMovement * appliedSpeed, ForceMode.VelocityChange);
         
             if (targetting.selectedTarget == null && relativeMovement != Vector3.zero && !isDodging)
                 transform.forward = relativeMovement.normalized;
             
         }
     }
Comment
Add comment · Show 2
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 spiceboy9994 · Oct 23, 2012 at 09:50 PM 0
Share

What about checking your Inputs. I do something like that in my game using my inputs like if (Input.GetAxis("Horizontal") == 0 { animation.Play("idle")). Not sure in your game what your input is... have you tried this?

avatar image asafsitner · Oct 23, 2012 at 09:52 PM 0
Share

@spiceboy9994:

I'd do this as well, but since he's using a rigidbody you can't know if it stopped moving just by checking the input. Take falling as example; even without user input it's clear that the object is moving.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by asafsitner · Oct 23, 2012 at 07:04 AM

Since you're using a rigidbody you can just test to see if `rigidbody.velocity == Vector3.Zero`

Another way is to cache the position from the last frame, compare it against the position in the current frame and see if there's any difference.

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
avatar image Condore · Oct 25, 2012 at 05:52 PM 0
Share

Thanks! I tried it out and this worked. I ended up having to create a new IEnumerator function that handles when my character transitions to Idle.

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

11 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

Related Questions

How to prevent rigidbody with capsule collider from walking off the edge of cube mesh if button held down? 1 Answer

How do I check for movement? 1 Answer

Velocity for movement 0 Answers

Powered Ragdoll? Direct Input for a Rigged Character 2 Answers

Smooth Player Ball Rolling 1 Answer


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