Input Control – Arrow Key – Advanced
#pragma strict //-------------------------------------------------------------------- // USER INPUT CONTROL START //-------------------------------------------------------------------- public var inputMoveSpeed : float = 10f; // increase -> faster public var inputTurnSpeed : float = 50f; // increase -> faster function UserInputControl() { // Arrow key to move object if(Input.GetKey(KeyCode.UpArrow)) transform.Translate(Vector2.up * inputMoveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.DownArrow)) transform.Translate(-1*Vector2.up * inputMoveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.RightArrow)) transform.Translate(Vector2.right * inputMoveSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.LeftArrow)) transform.Translate(-1*Vector2.right * inputMoveSpeed * Time.deltaTime); // Z X keys to rotate object if(Input.GetKey(KeyCode.X)) transform.Rotate(Vector3.back,inputTurnSpeed * Time.deltaTime); if(Input.GetKey(KeyCode.Z)) transform.Rotate(-1*Vector3.back,inputTurnSpeed * Time.deltaTime); } //-------------------------------------------------------------------- // USER INPUT CONTROL END //-------------------------------------------------------------------- function Update () { UserInputControl(); // call User Input control inside Update() }
Installation:
1. Copy paste the code
2. Call the function – UserInputControl(); – inside Update()