8-way movement (diagonal) idle issues
I am working on animations for 8-way movement for my characters. It was simple enough to get 4-way movement and idle animations set. I was also able to setup the other 4 directions and their idle animations. My issue is that when I am moving diagonally (e.g. A+W) and stop moving, the character does not stay facing diagonally, it reverts to left or up (A or W). The only way I can get it to idle in a diagonal direction is to try over and over again to release the keys at exactly the same time (which is not easy). I'm not sure if adding input lag will help with this or not. Does anyone have any advice? Any help is appreciated!
$$anonymous$$aybe remember(bool) the direction your walking ins$$anonymous$$d of the button presses. This might be a very unnecessary expense. Just my 2 cents :P.
Answer by tkati · Sep 12, 2016 at 01:03 AM
Why not map with 8 keys and 8 states? or 8 states where one of the keys sends the character "forward" and the state dictating which x/y change that would be?
using UnityEngine;
using System.Collections;
public abstract class ScrMovement : MonoBehaviour {
public float movementSpeed;
public float baseMovesPerTurn;
public float movementPts;
public static float xDistanceRowToNextRow = 0.5f;
public static float yDistanceColTonNextCol = 1.0f;
protected Animator animtr;
protected int faceDirection;
protected bool isTurning;
protected bool isAttacking;
protected bool isWalking;
// Use this for initialization
protected virtual void Start () {
animtr = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
public virtual void TurnObjCheck() { }
public virtual void MoveForward() { }
public virtual void UpdateFacingState(int change) { }
}
using UnityEngine;
using System.Collections;
public class ScrPlayer : ScrMovement {
private Rigidbody2D rb2;
// Use this for initialization
protected override void Start () {
base.Start();
rb2 = GetComponent<Rigidbody2D>();
faceDirection = 0;
isAttacking = false;
isTurning = false;
isWalking = false;
SetAnimator();
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
TurnObjCheck();
MoveForward();
}
public override void TurnObjCheck()
{
if (!isWalking && !isAttacking)
{
if (Input.GetKeyDown("q"))
{
isTurning = true;
UpdateFacingState(-1);
SetAnimator();
animtr.SetTrigger("TurnCounterClockwise");
}
else if (Input.GetKeyDown("e"))
{
isTurning = true;
UpdateFacingState(1);
SetAnimator();
animtr.SetTrigger("TurnClockwise");
}
isTurning = false;
}
}
public override void MoveForward()
{
if (Input.GetKeyDown("w") && !isTurning && !isAttacking)
{
isWalking = true;
SetAnimator();
switch (faceDirection)
{
case 0: animtr.SetTrigger("WalkS");
rb2.transform.Translate(new Vector3( movementSpeed * Time.deltaTime, 0.0f));
break;
case 1: animtr.SetTrigger("WalkSW"); break;
case 2: animtr.SetTrigger("WalkW"); break;
case 3: animtr.SetTrigger("WalkNW"); break;
case 4: animtr.SetTrigger("WalkN"); break;
case 5: animtr.SetTrigger("WalkNE"); break;
case 6: animtr.SetTrigger("WalkE"); break;
case 7: animtr.SetTrigger("WalkSE"); break;
default: break ;
};
isWalking = false;
SetAnimator();
}
}
public override void UpdateFacingState(int change)
{
faceDirection += change;
if (faceDirection < 0)
faceDirection = 7;
else if (faceDirection >= 8)
faceDirection = 0;
}
void SetAnimator()
{
animtr.SetBool("Attacking", isAttacking);
animtr.SetBool("Turning", isTurning);
animtr.SetBool("Walking", isWalking);
animtr.SetInteger("FaceDir", faceDirection);
}
}
Your answer
Follow this Question
Related Questions
How to make player rotate 180° and run that direction with mecanim + vice versa? 1 Answer
Character Animation Shake (MakeHuman + Unity MoCap Animations) 0 Answers
Keep position of CharacterController relative to rotating platform 0 Answers
Animation for each movement direction 0 Answers
Vector3.Distance returning offset values due to down force interference. 1 Answer