- Home /
Problem with diagonal movement + animations
Hey guys,
I'm starting to animate my character for my school project and I have a small problem, I have my animator, handling all of our characters animations based on how far you push the left thumbstick and I've got the animations crossfading between each other from "walk to "run" the only problem is, when I push the thumbstick diagonal so that it's in between the x and z axis, it puts my character back to walk, instead of run, even if I have the thumbstick pushed all the way.
how exactly can I fix this?
using UnityEngine;
using System.Collections;
public class TP_Animator : MonoBehaviour
{
public enum Direction
{
Forward, RightBackward, RightForward, Backward, LeftBackward, LeftForward, Right, Left, Stationary
}
public enum CharacterState
{
Idle, Walk, Run, Strafe, StartCombo, Combo1, Combo2
}
public ThirdPersonController Controller;
public static TP_Animator Instance;
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["attack1"].layer = 1;
animation["attack2"].layer = 2;
animation["attack3"].layer = 3;
}
void Start()
{
}
void Update()
{
DetermineCurrentMoveDirection();
DetermineCurrentState();
ProcessCurrentState();
}
public void DetermineCurrentMoveDirection()// this functions determines which direction the character is moving an plays animation
{
Debug.Log("Movement is occuring!!" + Controller.movement1);
if (Controller.movement1.x > 0)
{
// moving Right
if (Controller.movement1.z > 0f)
{
MoveDirection = Direction.RightForward;
}
else if (Controller.movement1.z < 0f)
{
MoveDirection = Direction.RightBackward;
}
else
{
MoveDirection = Direction.Right;
}
}
else if(Controller.movement1.x < 0)
{
//Move Left
if(Controller.movement1.z > -0)
{
MoveDirection = Direction.LeftBackward;
}
else if(Controller.movement1.z < -0)
{
MoveDirection = Direction.LeftForward;
}
else
{
MoveDirection = Direction.Left;
}
}
else
{
if(Controller.movement1.z > 0)
{
MoveDirection = Direction.Forward;
}
else if(Controller.movement1.z < 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)
{
case Direction.Stationary:
State = CharacterState.Idle;
break;
case Direction.Forward:
if(Controller.movement1.z * Controller.speed < Controller.speed * 0.5f)
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.RightForward:
if(Controller.movement1.z * Controller.speed < Controller.speed * 0.5f && JoyMag())
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.LeftForward:
if(-Controller.movement1.x * Controller.speed < Controller.speed * 0.5f && JoyMag())
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.Left:
if(-Controller.movement1.x * Controller.speed < Controller.speed * 0.5f)
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.Right:
if(Controller.movement1.x * Controller.speed < Controller.speed * 0.5f)
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.Backward:
if(-Controller.movement1.z * Controller.speed < Controller.speed * 0.5f)
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.LeftBackward:
if(-Controller.movement1.x * Controller.speed < Controller.speed * 0.5f && JoyMag())
State = CharacterState.Walk;
else
State = CharacterState.Run;
break;
case Direction.RightBackward:
if(-Controller.movement1.z * Controller.speed < Controller.speed * 0.5f)
State = CharacterState.Walk;
else
State = CharacterState.Run;
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()
{
//if(!animation.isPlaying)
//{
State = CharacterState.Run;
animation.CrossFade("run");
//}
}
void StartCombo()
{
//if (!animation.isPlaying)
{
State = CharacterState.StartCombo;
animation.CrossFade("attack1");
}
}
void Combo1()
{
//if (!animation.isPlaying)
{
State = CharacterState.Combo1;
animation.CrossFade("attack2");
}
}
void Combo2()
{
//if (!animation.isPlaying)
{
State = CharacterState.Combo2;
animation.CrossFade("attack3");
}
}
#endregion
#region Start Action Methods
public void Attack()
{
State = CharacterState.StartCombo;
animation.CrossFadeQueued("attack1", 0.2f, QueueMode.PlayNow);
}
public void Attack1()
{
State = CharacterState.Combo1;
animation.CrossFadeQueued("attack2", 0.1f, QueueMode.CompleteOthers);
}
public void Attack2()
{
State = CharacterState.Combo2;
animation.CrossFadeQueued("attack3", 0.1f, QueueMode.CompleteOthers);
}
#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.StartCombo:
StartCombo();
break;
case CharacterState.Combo1:
Combo1();
break;
case CharacterState.Combo2:
Combo2();
break;
}
}
public void JoyMag()
{
Vector2.SqrMagnitude(new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")));
}
}
Answer by ThermalFusion · Sep 07, 2012 at 09:40 PM
You're going in the right direction with the JoyMag function at the bottom, instead of switching between run and walk when only the x axis or z axis is below the threshold, do it with the magnitude of a Vector2 of the speed in x and z.
I thought I was going the right way, could you possibly include some pseudo code? I'd appreciate it.
I kind of understand what you're saying.
Calculate movement magnitude
if movement magnitude > run threshold
play run
else if movement magnitude > 0
play walk
else
play idle