using System.Collections; using System.Collections.Generic; using UnityEngine; using ZXKFramework; public class RotarySwitch : MonoBehaviour { private bool isUse = false; private Transform cameraMain; public Transform rotarySwitch;//跟随旋转的物体 private void OnMouseDown() { isUse = true; } private void OnMouseUp() { isUse = false; } void Start() { cameraMain = Camera.main.transform; } void Update() { if (!isUse|| rotarySwitch.IsNull()) return; //if (Input.GetMouseButton(0)) //{ // // 获取鼠标在屏幕上的位置 // Vector3 mousePos = Input.mousePosition; // // 将鼠标在屏幕上的位置转换为世界空间中的位置 // Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cameraMain.position.z)); // // 计算物体需要朝向的方向 // Vector3 direction = (worldPos - transform.position).normalized; // // 使用LookRotation方法将物体朝向鼠标的位置 // transform.rotation = Quaternion.LookRotation(direction, Vector3.up); //} //角色和鼠标的角度 Vector3 vpos3 = Camera.main.WorldToScreenPoint(transform.position);//角色屏幕坐标 float angle = Vector2.Angle(new Vector2(0, 1), Input.mousePosition - vpos3); Debug.LogError("Angle =" + angle); rotarySwitch.localEulerAngles = new Vector3(angle, rotarySwitch.localEulerAngles.y, rotarySwitch.localEulerAngles.z); } }