0
votes

Currently I am working on some player movements. The main idea of my player moves only horizontal and vertical, not diagonal. I couldn't find any reasonable solution for this problem. I really don't want to use rigidbody or character.controller for now. The other thing I want to achieve is, when I pressed multiple direction keys, I want my player move directly to last pressed direction. Here is my code:

using UnityEngine;
using System.Collections;


public class controller : MonoBehaviour {
    public int movementspeed;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {


        if (Input.GetKey (KeyCode.A))  {
            //ratation
            transform.localEulerAngles = new Vector3(0,270,0);
            //move
            transform.Translate (transform.right * movementspeed * Time.deltaTime); 

        }
        else if(Input.GetKey (KeyCode.D)) {
            //ratation
            transform.localEulerAngles = new Vector3(0,90,0);
            //move
            transform.Translate (transform.right *(-1)* movementspeed * Time.deltaTime);


        }
        else if(Input.GetKey (KeyCode.S)) {
            //ratation
            transform.localEulerAngles = new Vector3(0,180,0);
            //move
            transform.Translate (transform.forward * (-1) * movementspeed * Time.deltaTime);
        }
        else if (Input.GetKey (KeyCode.W)) {
            //ratation
            transform.localEulerAngles = new Vector3(0,0,0);
            //move
            transform.Translate (transform.forward * movementspeed * Time.deltaTime); 

        }
        else if (Input.GetKey (KeyCode.A))  {
            //ratation
            transform.localEulerAngles = new Vector3(0,270,0);
            //move
            transform.Translate (transform.right * movementspeed * Time.deltaTime); 

        }
    }
}
1

1 Answers

2
votes

The structure of your selection statement will not give you this functionality. As it will check the first if, then if false, it will check the next else if, and so on. So if I hold down A, no matter what I press, I will always only reach the A part of the code.

What I would do if I were you, is to add another layer on top of this. Only detect key down events, and set matching variables. When you set these, disable the others. Then use these variables for moving. Like this:

bool left, right, up, down;
void CheckInput() {
    if (Input.GetKeyDown(KeyCode.W) {
        up = true;
        left = right = down = false;
    }
    if (Input.GetKeyDown(KeyCode.S) {
        down = true;
        left = right = up = false;
    }
    if (Input.GetKeyDown(KeyCode.A) {
        left = true;
        right = up = down = false;
    }
    if (Input.GetKeyDown(KeyCode.D) {
        right= true;
        left= up = down = false;
    }
    //And then do matching OnKeyUp events to set them false
}

void Update() {
    CheckInput();
    if (left)
        //Move left
    if (right)
        //Move right
    //etc.
}