Files
Unity20GameJam/Assets/Gameplay/Scripts/Frist/CameraFollow.cs
2025-11-09 07:45:54 +08:00

67 lines
2.0 KiB
C#

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;
/// <summary>
/// 跟随目标
/// </summary>
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<Transform>();
// 遍历所有子级
}
void Start()
{
target = GameObject.Find("Player").transform;
Lastposition = target.position;
PlayerInput playerInput = FindAnyObjectByType<PlayerInput>();
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<Note>() != null)
// {
// if (obj.GetComponent<Note>().hitable)
// MusicGame.Instance.onNoteHit(obj.GetComponent<Note>().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;
}
}