using UnityEngine; using UnityEngine.InputSystem; public class CameraFollow : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created private InputAction lookAction; private Vector2 lookInput; /// /// 跟随目标 /// private Transform target; private Vector3 Lastposition; void Awake() { // 获取场景中的相机 Camera camera = Camera.main; // 获取相机的 Transform Transform cameraTransform = camera.transform; // 获取相机的父级 Transform parent = cameraTransform.parent; // 获取相机的父级下的所有子级 Transform[] children = parent?.GetComponentsInChildren(); // 遍历所有子级 } void Start() { target = GameObject.Find("Player").transform; Lastposition = target.position; PlayerInput playerInput = FindAnyObjectByType(); lookAction = playerInput.actions.FindAction("Attack"); lookAction.performed += (context) => { var mousePos = Mouse.current.position.ReadValue(); Ray ray = Camera.main.ScreenPointToRay(mousePos); Physics.Raycast(ray, out RaycastHit hit); if (hit.collider != null) { GameObject obj = hit.collider.gameObject; // if (obj.GetComponent() != null) // { // if (obj.GetComponent().hitable) // MusicGame.Instance.onNoteHit(obj.GetComponent().Uid); // } Debug.Log($"点击到了物体: {obj.name}"); } else { Debug.Log("未点击到任何物体。"); } }; } // Update is called once per frame void Update() { } void FixedUpdate() { var offset = Lastposition - target.position; Lastposition = target.position; transform.position += offset; } }