ui delay in progress

This commit is contained in:
yauwailam 2025-02-04 16:08:07 +08:00
parent 616beb5574
commit 75263c9d1b
3 changed files with 119 additions and 28 deletions

View file

@ -4,16 +4,77 @@ using UnityEngine;
public class Followplayercam : MonoBehaviour
{
public Transform playerCamera; // Assign the Main Camera (VR/XR Camera)
// 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
private Vector3 targetPosition;
void Start()
{
if (playerCamera == null)
{
Debug.LogError("Player Camera is not assigned!");
return;
}
// Set the initial position correctly
targetPosition = GetTargetPosition();
transform.position = targetPosition;
transform.rotation = GetTargetRotation();
}
void Update()
{
if (playerCamera != null)
{
// Make the canvas face the player's camera
transform.position = playerCamera.position + playerCamera.forward * 2f; // Adjust distance as needed
transform.LookAt(playerCamera);
transform.rotation = Quaternion.LookRotation(transform.position - playerCamera.position);
// Get the target position in world space
targetPosition = GetTargetPosition();
// Move UI smoothly to the target position
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
// Rotate UI smoothly to face the player
transform.rotation = Quaternion.Slerp(transform.rotation, GetTargetRotation(), Time.deltaTime * rotationSpeed);
}
}
// 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;
}
// 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);
}
}

View file

@ -240,24 +240,25 @@ MonoBehaviour:
m_LightCookieSize: {x: 1, y: 1}
m_LightCookieOffset: {x: 0, y: 0}
m_SoftShadowQuality: 0
--- !u!1 &143522624 stripped
--- !u!1 &331837452 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 7951603848978517227, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
m_CorrespondingSourceObject: {fileID: 740608301518776771, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
m_PrefabInstance: {fileID: 701711586}
m_PrefabAsset: {fileID: 0}
--- !u!114 &143522626
--- !u!114 &331837454
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 143522624}
m_GameObject: {fileID: 331837452}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 829050d920c070f4783f067cdcb74217, type: 3}
m_Name:
m_EditorClassIdentifier:
shakeAmount: 0.5
shakeMagnitude: 0.02
isShaking: 1
--- !u!1001 &701711586
PrefabInstance:
m_ObjectHideFlags: 0
@ -314,9 +315,9 @@ PrefabInstance:
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 7951603848978517227, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
- targetCorrespondingSourceObject: {fileID: 740608301518776771, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
insertIndex: -1
addedObject: {fileID: 143522626}
addedObject: {fileID: 331837454}
m_SourcePrefab: {fileID: 100100000, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
--- !u!43 &1212827717
Mesh:

View file

@ -532,7 +532,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
m_PresetInfoIsWorld: 1
--- !u!223 &222020873
Canvas:
m_ObjectHideFlags: 0
@ -542,8 +542,8 @@ Canvas:
m_GameObject: {fileID: 222020870}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 0
m_Camera: {fileID: 0}
m_RenderMode: 2
m_Camera: {fileID: 698240188}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
@ -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: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
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: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {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}
--- !u!114 &222020875
MonoBehaviour:
m_ObjectHideFlags: 0
@ -589,6 +589,10 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
playerCamera: {fileID: 698240182}
distanceFromPlayer: 2
heightOffset: -0.5
followSpeed: 2
rotationSpeed: 5
--- !u!1 &319734889
GameObject:
m_ObjectHideFlags: 0
@ -673,7 +677,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 618689981}
m_Enabled: 1
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 172515602e62fb746b5d573b38a5fe58, type: 3}
m_Name:
@ -683,6 +687,28 @@ MonoBehaviour:
blendDistance: 0
weight: 1
sharedProfile: {fileID: 11400000, guid: 655125311f4fc2048bcc12ddf6c49723, type: 2}
--- !u!20 &698240188 stripped
Camera:
m_CorrespondingSourceObject: {fileID: 4140838208988556472, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
m_PrefabInstance: {fileID: 701711586}
m_PrefabAsset: {fileID: 0}
--- !u!114 &698240189
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 618689981}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3abe37e4351124346aa2369f40796ac8, type: 3}
m_Name:
m_EditorClassIdentifier:
playerCamera: {fileID: 0}
distanceFromPlayer: 2
heightOffset: -0.5
followSpeed: 2
rotationSpeed: 5
--- !u!1001 &701711586
PrefabInstance:
m_ObjectHideFlags: 0
@ -693,15 +719,15 @@ PrefabInstance:
m_Modifications:
- target: {fileID: 6045648537344725186, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
propertyPath: m_LocalPosition.x
value: 6.598006
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6045648537344725186, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
propertyPath: m_LocalPosition.y
value: -0.13379598
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6045648537344725186, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
propertyPath: m_LocalPosition.z
value: -1.6521728
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6045648537344725186, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
propertyPath: m_LocalRotation.w
@ -742,6 +768,9 @@ PrefabInstance:
- targetCorrespondingSourceObject: {fileID: 7951603848978517227, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
insertIndex: -1
addedObject: {fileID: 698240187}
- targetCorrespondingSourceObject: {fileID: 7951603848978517227, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
insertIndex: -1
addedObject: {fileID: 698240189}
m_SourcePrefab: {fileID: 100100000, guid: a4a4b6738cb4ba34bb94baa14bd938f0, type: 3}
--- !u!1 &749638575
GameObject:
@ -1677,7 +1706,7 @@ Transform:
m_GameObject: {fileID: 773856643}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 5.46831, y: 0.009203672, z: -0.7732055}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.2105802, y: 0.25544837, z: 0.25544837}
m_ConstrainProportionsScale: 0
m_Children:
@ -1724,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: 5.6503806, y: 5.6503806, z: 5.6503806}
m_LocalScale: {x: 2.903561, y: 2.903561, z: 2.903561}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1701492195}
@ -1733,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.99994, y: 342}
m_AnchoredPosition: {x: 0.99988, y: 104}
m_SizeDelta: {x: 160.4619, y: 44.0223}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &917135294
@ -1803,7 +1832,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &983310690
MonoBehaviour:
m_ObjectHideFlags: 0
@ -2357,7 +2386,7 @@ Transform:
m_GameObject: {fileID: 1684186019}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 6.52, y: 0.01, z: -1.8}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 10.237, y: 10.237, z: 10.237}
m_ConstrainProportionsScale: 0
m_Children: []