This commit is contained in:
yauwailam 2025-02-05 15:56:52 +08:00
parent 75263c9d1b
commit 9918aed0ec
2 changed files with 31 additions and 57 deletions

View file

@ -4,36 +4,14 @@ using UnityEngine;
public class Followplayercam : MonoBehaviour public class Followplayercam : MonoBehaviour
{ {
// public Transform playerCamera; // Assign the Main Camera (VR/XR Camera) public Transform playerCamera;
// public float positionSmoothTime = 0.3f; // Adjust to control the delay public float distanceFromPlayer = 2.0f;
// public float rotationSmoothTime = 0.3f; // Adjust to control the rotation delay public float heightOffset = 0.0f;
// public float positionSmoothFactor = 0.2f; // Adjust for responsiveness
// private Vector3 velocity = Vector3.zero; public float rotationSmoothFactor = 0.2f;
//
// void Update()
// {
// if (playerCamera != null)
// {
// // Calculate the target position (a bit away from the player camera)
// Vector3 targetPosition = playerCamera.position + playerCamera.forward * 2f; // Adjust distance as needed
//
// // Smoothly interpolate the position to create a delay
// transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, positionSmoothTime);
//
// // Calculate the target rotation (look at the camera)
// Quaternion targetRotation = Quaternion.LookRotation(playerCamera.position - transform.position);
//
// // Smoothly interpolate the rotation to create a delay
// transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime / rotationSmoothTime);
// }
// }
public Transform playerCamera; // Assign the XR Camera (Main Camera in XR Rig)
public float distanceFromPlayer = 2.0f; // Distance in front of player
public float heightOffset = 0.0f; // Adjust if needed
public float followSpeed = 3.0f; // Speed of movement
public float rotationSpeed = 5.0f; // Speed of rotation
private Vector3 targetPosition; private Vector3 smoothedPosition;
private Vector3 velocity = Vector3.zero;
void Start() void Start()
{ {
@ -43,38 +21,34 @@ public class Followplayercam : MonoBehaviour
return; return;
} }
// Set the initial position correctly smoothedPosition = GetTargetPosition();
targetPosition = GetTargetPosition(); transform.position = smoothedPosition;
transform.position = targetPosition;
transform.rotation = GetTargetRotation(); transform.rotation = GetTargetRotation();
} }
void Update() void LateUpdate()
{ {
if (playerCamera != null) if (playerCamera != null)
{ {
// Get the target position in world space Vector3 targetPosition = GetTargetPosition();
targetPosition = GetTargetPosition();
// Move UI smoothly to the target position // SmoothDamp for a natural feel
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed); smoothedPosition = Vector3.SmoothDamp(smoothedPosition, targetPosition, ref velocity, positionSmoothFactor);
transform.position = smoothedPosition;
// Rotate UI smoothly to face the player // Exponential smoothing for rotation
transform.rotation = Quaternion.Slerp(transform.rotation, GetTargetRotation(), Time.deltaTime * rotationSpeed); transform.rotation = Quaternion.Slerp(transform.rotation, GetTargetRotation(), 1 - Mathf.Exp(-rotationSmoothFactor * Time.deltaTime * 10));
} }
} }
// Calculates world-space position (in front of player but stable)
private Vector3 GetTargetPosition() private Vector3 GetTargetPosition()
{ {
Vector3 forwardFlat = new Vector3(playerCamera.forward.x, 0, playerCamera.forward.z).normalized; // Keep level return playerCamera.position + playerCamera.forward * distanceFromPlayer + Vector3.up * heightOffset;
return playerCamera.position + forwardFlat * distanceFromPlayer + Vector3.up * heightOffset;
} }
// Calculates rotation so UI faces the player
private Quaternion GetTargetRotation() private Quaternion GetTargetRotation()
{ {
Vector3 lookAtPoint = new Vector3(playerCamera.position.x, transform.position.y, playerCamera.position.z); Vector3 lookAtPoint = new Vector3(playerCamera.position.x, transform.position.y, playerCamera.position.z);
return Quaternion.LookRotation(transform.position - lookAtPoint); return Quaternion.LookRotation(lookAtPoint - transform.position);
} }
} }

View file

@ -532,7 +532,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96 m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96 m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1 m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 1 m_PresetInfoIsWorld: 0
--- !u!223 &222020873 --- !u!223 &222020873
Canvas: Canvas:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -542,7 +542,7 @@ Canvas:
m_GameObject: {fileID: 222020870} m_GameObject: {fileID: 222020870}
m_Enabled: 1 m_Enabled: 1
serializedVersion: 3 serializedVersion: 3
m_RenderMode: 2 m_RenderMode: 1
m_Camera: {fileID: 698240188} m_Camera: {fileID: 698240188}
m_PlaneDistance: 100 m_PlaneDistance: 100
m_PixelPerfect: 0 m_PixelPerfect: 0
@ -565,7 +565,7 @@ RectTransform:
m_GameObject: {fileID: 222020870} m_GameObject: {fileID: 222020870}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
- {fileID: 917135293} - {fileID: 917135293}
@ -573,9 +573,9 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 353, y: 243.5} m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 706, y: 487} m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0, y: 0}
--- !u!114 &222020875 --- !u!114 &222020875
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -590,9 +590,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
playerCamera: {fileID: 698240182} playerCamera: {fileID: 698240182}
distanceFromPlayer: 2 distanceFromPlayer: 2
heightOffset: -0.5 heightOffset: -1
followSpeed: 2 positionSmoothFactor: 0.1
rotationSpeed: 5 rotationSmoothFactor: 0.1
--- !u!1 &319734889 --- !u!1 &319734889
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -707,8 +707,8 @@ MonoBehaviour:
playerCamera: {fileID: 0} playerCamera: {fileID: 0}
distanceFromPlayer: 2 distanceFromPlayer: 2
heightOffset: -0.5 heightOffset: -0.5
followSpeed: 2 positionSmoothFactor: 0.1
rotationSpeed: 5 rotationSmoothFactor: 0.1
--- !u!1001 &701711586 --- !u!1001 &701711586
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1753,7 +1753,7 @@ RectTransform:
m_GameObject: {fileID: 917135292} m_GameObject: {fileID: 917135292}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 2.903561, y: 2.903561, z: 2.903561} m_LocalScale: {x: 1.6460289, y: 1.6460289, z: 1.6460289}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
- {fileID: 1701492195} - {fileID: 1701492195}
@ -1762,7 +1762,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0.99988, y: 104} m_AnchoredPosition: {x: 0.9998495, y: 104}
m_SizeDelta: {x: 160.4619, y: 44.0223} m_SizeDelta: {x: 160.4619, y: 44.0223}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &917135294 --- !u!114 &917135294