In this script, I am aiming to get an object to move in 1 of 4 directions based on given input from the user. This is controlled by a switch statement which checks which respective button is being pressed. The recurring problem is that when the switch statement is called, all 4 parts(buttons/directions/input) of the switch statement run simultaneously. This makes the object never move because the code is trying to make it go up, down, left, and right all at the same time. I pin-pointed the problem to switch statement because when 3 of the buttons(cases) are commented it out, it moves in the desired direction that was left un-commented.
using UnityEngine;
using System.Collections;
public class MovementInput : MonoBehaviour {
public enum ButtonType{Up, Down, Left, Right};
public ButtonType button;
static bool mover = false;
public float speed = 20;
public GameObject player;
float myX, myY;
// Use this for initialization
public void reset () {
mover = false;
}
// Update is called once per frame
public void move () {
mover = true;
}
void Update(){
if(mover){
switch (button){
case ButtonType.Down:
myY = -speed;
Debug.Log ("down");
break;
case ButtonType.Left:
myX = -speed;
Debug.Log ("left");
break;
case ButtonType.Right:
myX = speed;
Debug.Log ("right");
break;
case ButtonType.Up:
myY = speed;
Debug.Log ("up");
break;
}
} else {
myX = 0;
myY= 0;
}
player.transform.Translate(myX * Time.deltaTime, myY * Time.deltaTime, 0);
}
}