using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scripting; public class CameraKeyController : MonoBehaviour { public float Height; public float Speed; public float RotaionSpeed; public Scpoe RotationScpoe; private Ray ray; private RaycastHit hit; private Ray ray2; private RaycastHit hit2; private Vector2 MouseLValue; // 鼠标左键变量 private Vector2 MouseRValue; // 鼠标右键变量 private float MouseCValue; // 鼠标中键变量 private Transform CurrentScpoe; private void Start() { ray = new Ray(transform.position, Vector3.down); if (Physics.Raycast(ray, out hit)) { if (hit.transform.tag == "Scpoe") { CurrentScpoe = hit.transform; CurrentScpoe.GetComponent().size = new Vector3(1, 0.9f, 1); } } } void RayTest() { ray = new Ray(transform.position, Vector3.down); Debug.DrawRay(ray.origin, ray.direction * 100, Color.red); if (Physics.Raycast(ray, out hit)) { transform.position = hit.point + new Vector3(0, Height, 0); Vector3 position = transform.position + Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized * Input.GetAxis("Vertical") * Time.deltaTime * Speed + Vector3.ProjectOnPlane(transform.right, Vector3.up).normalized * Input.GetAxis("Horizontal") * Time.deltaTime * Speed; if (hit.transform.tag == "Scpoe") { if (CurrentScpoe != hit.transform) { CurrentScpoe.GetComponent().size = Vector3.one; CurrentScpoe = hit.transform; CurrentScpoe.GetComponent().size = new Vector3(1.1f, 0.9f, 1.1f); } MoveScpoe(CurrentScpoe.transform, ref position); Move(position); } } } /// /// 移动范围限制 /// /// 限制范围 /// 位置 void MoveScpoe(Transform Scpoe, ref Vector3 Position) { Vector3 SP_Dir = Position - Scpoe.position; SP_Dir = Vector3.ProjectOnPlane(SP_Dir, Scpoe.up); float Forward = Vector3.Dot(SP_Dir, Scpoe.forward); float Right = Vector3.Dot(SP_Dir, Scpoe.right); Forward = Mathf.Max(Mathf.Min(Forward, Scpoe.localScale.z * 0.5f), -Scpoe.localScale.z * 0.5f); Right = Mathf.Max(Mathf.Min(Right, Scpoe.localScale.x * 0.5f), -Scpoe.localScale.x * 0.5f); Position = Scpoe.position + Scpoe.right * Right + Scpoe.up * Height + Scpoe.forward * Forward; } void RotationController() { if (Input.GetMouseButton(1)) { /* transform.forward += transform.right * MouseRValue.x; transform.forward += transform.up * MouseRValue.y; */ transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + new Vector3(-MouseRValue.y, MouseRValue.x, 0) * 100); } } void Move(Vector3 position) { transform.position = position; } /// /// 鼠标变量更新 /// void MouseValueUpdate() { if (Input.GetMouseButton(0)) MouseLValue = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * Time.deltaTime; MouseLValue = Vector2.Lerp(MouseLValue, Vector2.zero, Time.deltaTime); if (Input.GetMouseButton(1)) MouseRValue = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * Time.deltaTime; MouseRValue = Vector2.Lerp(MouseRValue, Vector2.zero, Time.deltaTime); MouseCValue -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime; MouseCValue = Mathf.Lerp(MouseCValue, 0, Time.deltaTime); } private void Update() { MouseValueUpdate(); RayTest(); RotationController(); } }