2

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

Redza R.
  • 21
  • 2
  • The mistake was with the code in the second "if" statement. I should have used playerRigidbody.velocity.x instead of playerRigidbody.velocity.y. Oopsies. – Redza R. Aug 26 '18 at 06:35

2 Answers2

3

The whole logic seems strange to me, I would make a single vector of movement

 playerRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, Input.GetAxisRaw("Vertical") * moveSpeed);

unless there are some other concerns.

It's possible that with so many conditions only last 2 of 4 are end being applied.

Timofeus
  • 181
  • 1
  • 9
  • Though I'm glad, I'm kind of embarassed to admit that I found the problem. I accident used "playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.y, Input.GetAxisRaw("Vertical") * moveSpeed);". When it actuality, it should have been playerRigidbody.velocity.x. – Redza R. Aug 26 '18 at 06:34
0

The mistake was with the code in the second "if" statement. I should have used playerRigidbody.velocity.x instead of playerRigidbody.velocity.y. Oopsies.

Redza R.
  • 21
  • 2