I've been trying to find a solution for this issue for a while now with no luck.
I want to be able to move in 8 directions but for some odd reason, my player only wants to move in 6 directions.
When I press:
W+D or W+A, it moves towards the top right.
S+D or S+A, it moves towards the bottom left.
The vertical and horizontal movements work perfectly fine. It's just two of the four diagonal movements which are being a pain.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerTest : MonoBehaviour
{
public float moveSpeed;
private Animator anim;
private Rigidbody2D playerRigidbody;
private bool playerMoving;
public Vector2 lastMove;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
playerMoving = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
playerRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, playerRigidbody.velocity.y);
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.y, Input.GetAxisRaw("Vertical") * moveSpeed);
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
playerRigidbody.velocity = new Vector2(0f, playerRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, 0f);
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("PlayerMoving", playerMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
}
}
This is the basic code that I used for my character movement. If someone could help me fix this, it would be greatly appreciated.
Thanks, Redza