- Home /
How to keep Diagonal Movement Direction in 2D Isometric game?
Hello! I'm current making a simple 2D Isometric game and I've made a movement script with correlating animations. I'm using a nested blend tree to determine the direction im going and how fast I'm going. If my current player speed is >0 then the walking animation triggers. If the current player speed is 0 I play the idle animation. This all works perfectly fine, the only issue is that when I move diagonally my diagonal position isn't held. I believe this is because I use WASD to determine Input and was I press "W+D" to move up and to the right for example, it well sometimes make my idle right facing animation play instead of my up right animation play. I'm thinking that I would need to store my current direction I'm facing or I would need to create a bool to check if I'm diagonal but I'm really unsure. Please let me know if you have any idea's or suggestions! Thank you in advance.!
Code -----
private float baseMoveSpeed = 5;
public float dashDistance = 5;
private Vector3 playerPos;
public string prevMoveDir;
private Rigidbody2D rigidbody2D;
private Vector3 moveDir;
public bool isDashing;
public bool isRunning;
public Animator animator;
public Vector2 animatorMoveDirection;
public float animatorMoveSpeed;
// Start is called before the first frame update
private void Awake()
{
rigidbody2D = GetComponent<Rigidbody2D>();
}
void Start()
{
playerPos = transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
//speed = moveX + moveY;
rigidbody2D.velocity = moveDir * baseMoveSpeed;
if(isDashing)
{
rigidbody2D.MovePosition(transform.position + moveDir * dashDistance);
isDashing = false;
}
if(isRunning)
{
rigidbody2D.velocity = moveDir * (baseMoveSpeed * 1.5f);
isRunning = false;
}
}
private void Update()
{
float moveX = 0;
float moveY = 0;
if (Input.GetKey(KeyCode.W)) // Get key down used for single press and not constant pressing
{
moveY += 1f;
}
if (Input.GetKey(KeyCode.S)) // Get key down used for single press and not constant pressing
{
moveY -= 1f;
}
if (Input.GetKey(KeyCode.D)) // Get key down used for single press and not constant pressing
{
moveX += 1f;
}
if (Input.GetKey(KeyCode.A)) // Get key down used for single press and not constant pressing
{
moveX -= 1f;
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift)) // Get key down used for single press and not constant pressing
{
moveY += 1f;
isRunning = true;
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.LeftShift)) // Get key down used for single press and not constant pressing
{
moveY -= 1f;
isRunning = true;
}
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.LeftShift)) // Get key down used for single press and not constant pressing
{
moveX += 1f;
isRunning = true;
}
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift)) // Get key down used for single press and not constant pressing
{
moveX -= 1f;
isRunning = true;
}
if (Input.GetKeyDown(KeyCode.Space))
{
isDashing = true;
}
moveDir = new Vector3(moveX, moveY).normalized;
animatorMoveDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
animatorMoveSpeed = animatorMoveDirection.magnitude;
Animate();
}
void Animate()
{
if(animatorMoveDirection != Vector2.zero)
{
animator.SetFloat("Horizontal", animatorMoveDirection.x);
animator.SetFloat("Vertical", animatorMoveDirection.y);
}
animator.SetFloat("Speed", animatorMoveDirection.magnitude);
}
}
I would just store the direction, you could store it as a normalized vector even. or just an int, 0-7, or an enum for readability..
Your answer
Follow this Question
Related Questions
Why can't you use the bone influence tool in the Sprite Skinning Editor? 0 Answers
help with freezing 2D sprite rotation 2 Answers
4 direction animation unity 2d 3 Answers
[2D] Unity Animation and CPU usage of EXE. Best way to do? 0 Answers
Problem with 2D gun rotation script (Would really apreaciate help) 1 Answer