using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeController : MonoBehaviour { float rotSpeed = 90; public int rot_type = 1; // 0: 固定90角度 1: 自由角度 public int rot_angle = 0; public int angle_rotation_delta = 10; void Update() { if (rot_type == 0) { if (Input.GetKey(KeyCode.UpArrow)) { transform.Rotate(this.rotSpeed, 0, 0); } if (Input.GetKey(KeyCode.DownArrow)) { transform.Rotate(-this.rotSpeed, 0, 0); } if (Input.GetKey(KeyCode.LeftArrow)) { transform.Rotate(0, this.rotSpeed, 0); } if (Input.GetKey(KeyCode.RightArrow)) { transform.Rotate(0, -this.rotSpeed, 0); } } if (rot_type == 1) { // 比較Input.GetKey & Input.GetKeyDown的差異性 if (Input.GetKeyDown(KeyCode.UpArrow)) { rot_angle += angle_rotation_delta; this.transform.rotation = Quaternion.Euler(new Vector3(rot_angle, 0, 0)); } if (Input.GetKeyDown(KeyCode.DownArrow)) { rot_angle -= angle_rotation_delta; this.transform.rotation = Quaternion.Euler(new Vector3(rot_angle, 0, 0)); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { rot_angle -= angle_rotation_delta; this.transform.rotation = Quaternion.Euler(new Vector3(0, rot_angle, 0)); } if (Input.GetKeyDown(KeyCode.RightArrow)) { rot_angle += angle_rotation_delta; this.transform.rotation = Quaternion.Euler(new Vector3(0, rot_angle, 0)); } } if (Input.GetKey(KeyCode.X)) { upsee(); } if (Input.GetKey(KeyCode.Space)) { //直接旋轉至原點 this.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); } } public void upsee() //上視圖 { this.transform.rotation = Quaternion.Euler(new Vector3(-90.0f, 0, 0)); } public void downsee() //下視圖 { this.transform.rotation = Quaternion.Euler(new Vector3(90.0f, 0, 0)); } public void leftsee() //左視圖 { this.transform.rotation = Quaternion.Euler(new Vector3(0, -90.0f, 0)); } public void rightsee() //右視圖 { this.transform.rotation = Quaternion.Euler(new Vector3(0, 90.0f, 0)); } }