- Home /
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;
}
}
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?
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.
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.
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.