using UnityEngine; using System.Collections; public class PlayerControl : MonoBehaviour { public CameraClass camera; public bool LEFT, RIGHT, UP, DOWN; private const float WALKSPEED = 5.0f; //private float DIAGWALKSPEED = Mathf.Sqrt(WALKSPEED); //unused private float speed; // Use this for initialization void Start () { } // Update is called once per frame void Update () { updateKeys(); int dir = -1; //initialize movement angle variable if(RIGHT && !LEFT) { dir = 6; speed = WALKSPEED; } else if(LEFT && !RIGHT) { dir = 2; speed = WALKSPEED; } if(UP && !DOWN) { speed = WALKSPEED; if(dir == 2) { dir += 1; } else if(dir == 6) { dir -= 1; } else { dir = 4; } } else if(DOWN && !UP) { speed = WALKSPEED; if(dir == 2) { dir -= 1; } else if(dir == 6) { dir += 1; } else { dir = 0; } } if(dir != -1) { float dir_angle = camera.getDirection(dir); Vector3 direction = new Vector3(Mathf.Sin(dir_angle), 0.0f, Mathf.Cos(dir_angle)); transform.Translate(direction * Time.deltaTime * speed); camera.updateCamera(); //Ensures that the camera position doesn't lag behind the player } else { speed = 0; } } void updateKeys() { LEFT = Input.GetKey("left"); RIGHT = Input.GetKey("right"); UP = Input.GetKey("up"); DOWN = Input.GetKey("down"); } public float getSpeed() { return speed; } }