2025-01-31 17:56:22 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class Followplayercam : MonoBehaviour
|
|
|
|
{
|
2025-02-07 11:26:41 +08:00
|
|
|
// public Transform playerCamera;
|
|
|
|
// public float distanceFromPlayer = 2.0f;
|
|
|
|
// public float heightOffset = 0.0f;
|
|
|
|
// public float positionSmoothTime = 0.3f; // Controls delay for position
|
|
|
|
// public float rotationSmoothTime = 0.15f; // Controls delay for rotation
|
|
|
|
//
|
|
|
|
// private Vector3 smoothedPosition;
|
|
|
|
// private Vector3 velocity = Vector3.zero;
|
|
|
|
//
|
|
|
|
// void Start()
|
|
|
|
// {
|
|
|
|
// if (playerCamera == null)
|
|
|
|
// {
|
|
|
|
// Debug.LogError("Player Camera is not assigned!");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// smoothedPosition = GetTargetPosition();
|
|
|
|
// transform.position = smoothedPosition;
|
|
|
|
// transform.rotation = GetTargetRotation();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// void LateUpdate()
|
|
|
|
// {
|
|
|
|
// if (playerCamera != null)
|
|
|
|
// {
|
|
|
|
// Vector3 targetPosition = GetTargetPosition();
|
|
|
|
//
|
|
|
|
// // Apply smooth damping for slight delay
|
|
|
|
// smoothedPosition = Vector3.SmoothDamp(
|
|
|
|
// smoothedPosition,
|
|
|
|
// targetPosition,
|
|
|
|
// ref velocity,
|
|
|
|
// positionSmoothTime
|
|
|
|
// );
|
|
|
|
// transform.position = smoothedPosition;
|
|
|
|
//
|
|
|
|
// // Apply smooth rotation with slight delay
|
|
|
|
// transform.rotation = Quaternion.Slerp(
|
|
|
|
// transform.rotation,
|
|
|
|
// GetTargetRotation(),
|
|
|
|
// Time.deltaTime / rotationSmoothTime
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private Vector3 GetTargetPosition()
|
|
|
|
// {
|
|
|
|
// return playerCamera.position + playerCamera.forward * distanceFromPlayer + Vector3.up * heightOffset;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private Quaternion GetTargetRotation()
|
|
|
|
// {
|
|
|
|
// Vector3 lookAtPoint = new Vector3(playerCamera.position.x, transform.position.y, playerCamera.position.z);
|
|
|
|
// return Quaternion.LookRotation(lookAtPoint - transform.position);
|
|
|
|
// }
|
|
|
|
[Header("Target to Follow")]
|
|
|
|
public Transform target; // Usually the Main Camera (XR Rig's Head)
|
2025-02-04 16:08:07 +08:00
|
|
|
|
2025-02-07 11:26:41 +08:00
|
|
|
[Header("Follow Settings")]
|
|
|
|
public float followSpeed = 5f; // How quickly it follows the target
|
|
|
|
public Vector3 offset = new Vector3(0, -0.2f, 1.5f); // Position offset
|
|
|
|
public bool followRotation = true; // Toggle for rotating with the head
|
2025-02-04 16:08:07 +08:00
|
|
|
|
2025-02-07 11:26:41 +08:00
|
|
|
private void LateUpdate()
|
2025-02-04 16:08:07 +08:00
|
|
|
{
|
2025-02-07 11:26:41 +08:00
|
|
|
if (target == null) return;
|
2025-02-04 16:08:07 +08:00
|
|
|
|
2025-02-07 11:26:41 +08:00
|
|
|
// Smooth Position Follow
|
|
|
|
Vector3 desiredPosition = target.position + target.TransformDirection(offset);
|
|
|
|
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime * followSpeed);
|
2025-01-31 17:56:22 +08:00
|
|
|
|
2025-02-07 11:26:41 +08:00
|
|
|
// Smooth Rotation Follow
|
|
|
|
if (followRotation)
|
2025-01-31 17:56:22 +08:00
|
|
|
{
|
2025-02-07 11:26:41 +08:00
|
|
|
Quaternion desiredRotation = Quaternion.Euler(0, target.eulerAngles.y, 0);
|
|
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, Time.deltaTime * followSpeed);
|
2025-01-31 17:56:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|