|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | + |
| 4 | +public class CameraBehaviourScript : MonoBehaviour |
| 5 | +{ |
| 6 | + |
| 7 | + public GameObject player; //母球 |
| 8 | + |
| 9 | + private Vector3 offset; //攝影機 離 母球 相對位置 |
| 10 | + public float speed = 6.0f; //母球 初速 |
| 11 | + private float force = 0.0f; //母球 蓄力 大小 |
| 12 | + public float forceSpd = 3.0f; //母球 蓄力 速度 |
| 13 | + |
| 14 | + public float distance = 6.0f; //攝影機 離 母球 距離 初始值 |
| 15 | + public float xSpeed = 120.0f; //滑鼠左右移動速度 |
| 16 | + public float ySpeed = 120.0f; //滑鼠上下移動速度 |
| 17 | + |
| 18 | + public float yMinLimit = -20f; //滑鼠上下 轉仰角 下限 |
| 19 | + public float yMaxLimit = 80f; //滑鼠上下 轉仰角 上限 |
| 20 | + |
| 21 | + public float distanceMin = .5f; //滾輪 拉 攝影機 離 母球 距離下限 |
| 22 | + public float distanceMax = 15f; //滾輪 拉 攝影機 離 母球 距離上限 |
| 23 | + |
| 24 | + private Rigidbody rbody; |
| 25 | + |
| 26 | + float x = 0.0f; |
| 27 | + float y = 0.0f; |
| 28 | + // Use this for initialization |
| 29 | + void Start() |
| 30 | + { |
| 31 | + //攝影機位置 - 母球位置 = 相對位置 |
| 32 | + offset = transform.position - player.transform.position; |
| 33 | + Vector3 angles = transform.eulerAngles; //攝影機角度 |
| 34 | + x = angles.y; |
| 35 | + y = angles.x; |
| 36 | + |
| 37 | + rbody = player.GetComponent<Rigidbody>(); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + // Update is called once per frame |
| 42 | + void LateUpdate() |
| 43 | + { |
| 44 | + if (Input.GetMouseButton(0)) |
| 45 | + { |
| 46 | + x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f; |
| 47 | + y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; |
| 48 | + y = ClampAngle(y, yMinLimit, yMaxLimit); // 限制 仰角 傾仰範圍 |
| 49 | + //繞 Y軸 是 繞球轉,繞X軸 是 傾仰 |
| 50 | + Quaternion rotation = Quaternion.Euler(y, x, 0); |
| 51 | + distance = Mathf.Clamp( // 限制 滾輪 拉 遠近 移動範圍 |
| 52 | + distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax); |
| 53 | + // (沿Z軸 前後移動) |
| 54 | + Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); offset = rotation * negDistance; //依新角度,距離 重新算相對位置 |
| 55 | + transform.rotation = rotation; // 攝影機 新角度 |
| 56 | + } |
| 57 | + // 攝影機新位置 = 新相對位置 + 母球位置 |
| 58 | + transform.position = player.transform.position + offset; |
| 59 | + if (Input.GetMouseButton(1)) // 按滑鼠右鍵 按住 蓄力 |
| 60 | + { |
| 61 | + force += Time.deltaTime * forceSpd; // 大小和時間成正比 |
| 62 | + } |
| 63 | + else if (Input.GetMouseButtonUp(1)) // 按滑鼠右鍵 放開 發射 |
| 64 | + { |
| 65 | + //眼睛看的方向the direction of camera(eye): |
| 66 | + // Camera.main.transform.forward |
| 67 | + Vector3 movement = Camera.main.transform.forward; |
| 68 | + movement.y = 0.0f; // no vertical movement 不上下移動 |
| 69 | + //力量模式impulse:衝力,speed:初速大小 |
| 70 | + rbody.AddForce(movement * speed * force, ForceMode.Impulse); |
| 71 | + force = 0.0f; // 力量用盡歸0,準備下次重新蓄力 |
| 72 | + } |
| 73 | + } |
| 74 | + public static float ClampAngle(float angle, float min, float max) |
| 75 | + { // 用上下限 夾值 |
| 76 | + if (angle < -360F) |
| 77 | + angle += 360F; |
| 78 | + if (angle > 360F) |
| 79 | + angle -= 360F; |
| 80 | + return Mathf.Clamp(angle, min, max); |
| 81 | + } |
| 82 | +} |
| 83 | + |
0 commit comments