diff --git a/Game/Assets/Followplayercam.cs b/Game/Assets/Followplayercam.cs index e665ceb..1b333e5 100644 --- a/Game/Assets/Followplayercam.cs +++ b/Game/Assets/Followplayercam.cs @@ -4,36 +4,14 @@ using UnityEngine; public class Followplayercam : MonoBehaviour { - // public Transform playerCamera; // Assign the Main Camera (VR/XR Camera) - // public float positionSmoothTime = 0.3f; // Adjust to control the delay - // public float rotationSmoothTime = 0.3f; // Adjust to control the rotation delay - // - // private Vector3 velocity = Vector3.zero; - // - // 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 + public Transform playerCamera; + public float distanceFromPlayer = 2.0f; + public float heightOffset = 0.0f; + public float positionSmoothFactor = 0.2f; // Adjust for responsiveness + public float rotationSmoothFactor = 0.2f; - private Vector3 targetPosition; + private Vector3 smoothedPosition; + private Vector3 velocity = Vector3.zero; void Start() { @@ -43,38 +21,34 @@ public class Followplayercam : MonoBehaviour return; } - // Set the initial position correctly - targetPosition = GetTargetPosition(); - transform.position = targetPosition; + smoothedPosition = GetTargetPosition(); + transform.position = smoothedPosition; transform.rotation = GetTargetRotation(); } - void Update() + void LateUpdate() { if (playerCamera != null) { - // Get the target position in world space - targetPosition = GetTargetPosition(); + Vector3 targetPosition = GetTargetPosition(); - // Move UI smoothly to the target position - transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed); + // SmoothDamp for a natural feel + smoothedPosition = Vector3.SmoothDamp(smoothedPosition, targetPosition, ref velocity, positionSmoothFactor); + transform.position = smoothedPosition; - // Rotate UI smoothly to face the player - transform.rotation = Quaternion.Slerp(transform.rotation, GetTargetRotation(), Time.deltaTime * rotationSpeed); + // Exponential smoothing for rotation + 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() { - Vector3 forwardFlat = new Vector3(playerCamera.forward.x, 0, playerCamera.forward.z).normalized; // Keep level - return playerCamera.position + forwardFlat * distanceFromPlayer + Vector3.up * heightOffset; + return playerCamera.position + playerCamera.forward * distanceFromPlayer + Vector3.up * heightOffset; } - // Calculates rotation so UI faces the player private Quaternion GetTargetRotation() { 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); } } diff --git a/Game/Assets/Scenes/Wai Lam's interaction.unity b/Game/Assets/Scenes/Wai Lam's interaction.unity index 814f33d..dd9c967 100644 --- a/Game/Assets/Scenes/Wai Lam's interaction.unity +++ b/Game/Assets/Scenes/Wai Lam's interaction.unity @@ -532,7 +532,7 @@ MonoBehaviour: m_FallbackScreenDPI: 96 m_DefaultSpriteDPI: 96 m_DynamicPixelsPerUnit: 1 - m_PresetInfoIsWorld: 1 + m_PresetInfoIsWorld: 0 --- !u!223 &222020873 Canvas: m_ObjectHideFlags: 0 @@ -542,7 +542,7 @@ Canvas: m_GameObject: {fileID: 222020870} m_Enabled: 1 serializedVersion: 3 - m_RenderMode: 2 + m_RenderMode: 1 m_Camera: {fileID: 698240188} m_PlaneDistance: 100 m_PixelPerfect: 0 @@ -565,7 +565,7 @@ RectTransform: m_GameObject: {fileID: 222020870} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_Children: - {fileID: 917135293} @@ -573,9 +573,9 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 353, y: 243.5} - m_SizeDelta: {x: 706, y: 487} - m_Pivot: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0, y: 0} --- !u!114 &222020875 MonoBehaviour: m_ObjectHideFlags: 0 @@ -590,9 +590,9 @@ MonoBehaviour: m_EditorClassIdentifier: playerCamera: {fileID: 698240182} distanceFromPlayer: 2 - heightOffset: -0.5 - followSpeed: 2 - rotationSpeed: 5 + heightOffset: -1 + positionSmoothFactor: 0.1 + rotationSmoothFactor: 0.1 --- !u!1 &319734889 GameObject: m_ObjectHideFlags: 0 @@ -707,8 +707,8 @@ MonoBehaviour: playerCamera: {fileID: 0} distanceFromPlayer: 2 heightOffset: -0.5 - followSpeed: 2 - rotationSpeed: 5 + positionSmoothFactor: 0.1 + rotationSmoothFactor: 0.1 --- !u!1001 &701711586 PrefabInstance: m_ObjectHideFlags: 0 @@ -1753,7 +1753,7 @@ RectTransform: m_GameObject: {fileID: 917135292} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_Children: - {fileID: 1701492195} @@ -1762,7 +1762,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {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_Pivot: {x: 0.5, y: 0.5} --- !u!114 &917135294