| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using UnityEngine;
- public class KeyboardBowController : MonoBehaviour
- {
- [Header("W/S 上下、A/D 左右、Q/ 射击/ 、Y/U 胜利/失败")]
- public float rotateSpeed = 60f; // 每秒旋转角度
- public bool smooth = false; // 是否平滑旋转
- public float smoothSpeed = 10f;
- private Quaternion targetRotation;
- void Start()
- {
- if (CameraToLook.ins == null)
- {
- Debug.LogError("CameraToLook.ins 未找到!");
- enabled = false;
- return;
- }
- targetRotation = CameraToLook.ins.localRotation;
- }
- void Update()
- {
- HandleKeyboardInput();
- if (smooth)
- {
- CameraToLook.ins.localRotation =
- Quaternion.Slerp(CameraToLook.ins.localRotation, targetRotation, Time.deltaTime * smoothSpeed);
- }
- else
- {
- CameraToLook.ins.localRotation = targetRotation;
- }
- }
- /**
- * 如果是屏幕坐标
- * Ray ray = Camera.main.ScreenPointToRay(point);
- Vector3 rayEndPoint = ray.GetPoint(200);
- Quaternion quat = Quaternion.LookRotation(rayEndPoint - Camera.main.transform.position);
- // 挑战场景中其相机的父级有旋转,需要换算
- quat = Quaternion.AngleAxis(-180, Vector3.up) * quat;
- if (CameraToLook.ins != null) CameraToLook.ins.localRotation = quat;
- */
- void HandleKeyboardInput()
- {
- float delta = rotateSpeed * Time.deltaTime;
- // 上下(X轴旋转)
- if (Input.GetKey(KeyCode.W))
- targetRotation *= Quaternion.Euler(-delta, 0, 0);
- if (Input.GetKey(KeyCode.S))
- targetRotation *= Quaternion.Euler(delta, 0, 0);
- // 左右(Y轴旋转)
- if (Input.GetKey(KeyCode.A))
- targetRotation *= Quaternion.Euler(0, -delta, 0);
- if (Input.GetKey(KeyCode.D))
- targetRotation *= Quaternion.Euler(0, delta, 0);
- }
- }
|