简体   繁体   中英

Unity Top Down Game Movement help needed

I just started coding 3 days ago and in my game I only want 4 movement-directions instead of 8. I have a fully working code with animations and walking logic, but I am to nooby make a Code by my own, because I just started.

So could someone modify my code, so that I can only go in 4-directions. Thank you:)

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    private Animator anim;
    
    private float x, y;
    
    private bool isWalking;
    
    public float moveSpeed;
    
    void Start()
    {
        anim = GetComponent<Animator>();
    }
    
    void Update()
    {
        x = Input.GetAxis("Horizontal");
        y = Input.GetAxis("Vertical");
    
        if (x != 0 || y != 0)
        {
            if (!isWalking)
            {
                isWalking = true;
                anim.SetBool("isWalking", isWalking);
            }
    
            Move();
        }
        else
        {
            if (isWalking)
            {
                isWalking = false;
                anim.SetBool("isWalking", isWalking);
            }
        }
    }
    
    private void Move()
    {
        anim.SetFloat("X", x);
        anim.SetFloat("Y", y);
        
        transform.Translate(x * Time.deltaTime * moveSpeed,
            y * Time.deltaTime * moveSpeed,
            0);
    }
}

Use XOR operator.

if (x != 0 ^ y != 0)
{
   // moving..
}

I wrote 2 different (but fairly similar) options for moving in just 4 directions.

First option, just pick which axis has the highest value and move that direction (if using a controller, this may or may not be work the way you want).

Second and simpler option, keep moving the player on its current axis regardless if another axis has a higher value.

Both code options are there and you can play around with either option. Hopefully this helps start you on which path you want to take.

public class PlayerMovement : MonoBehaviour {
    private Animator anim;
    private float x, y;
    private bool isWalking;
    private bool movingX = false;
    
    public float moveSpeed;
    
    void Start() {
        anim = GetComponent<Animator>();
    }
    
    void Update() {
        x = Input.GetAxis("Horizontal");
        y = Input.GetAxis("Vertical");
    
        if (x != 0 || y != 0) {
            if (!isWalking) {
                isWalking = true;
                anim.SetBool("isWalking", isWalking);
            }
    
            Move();
        } else {
            if (isWalking)  {
                isWalking = false;
                anim.SetBool("isWalking", isWalking);
            }
        }
    }
    
    private void Move() {
        // whichever direction has the highest value, will be the direction we go.
        ///*
        if (Mathf.Abs(x) > Mathf.Abs(y)) {
            movingX = true;
            y = 0;
        } else if (Mathf.Abs(x) < Mathf.Abs(y)) {
            movingX = false;
            x = 0;
        } else {
            // keeps going same direction if both maxed.
            if (movingX) {
                y = 0;
            } else {
                x = 0;
            }
        }
        //*/

        // or simple, just keep going in same direction until player stops using axis.
        /*
        if (x != 0 && y == 0) {
            movingX = true;
        } else if (x == 0 && y != 0) {
            movingX = false;
        } 

        if (movingX) {
            y = 0;
        } else {
            x = 0;
        }
        */
        
        anim.SetFloat("X", x);
        anim.SetFloat("Y", y);
        
        transform.Translate(x * Time.deltaTime * moveSpeed,
            y * Time.deltaTime * moveSpeed,
            0);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM