Compare commits

...

2 commits

Author SHA1 Message Date
3465373560 game(scripts): standardise Car 2025-02-14 22:13:03 +08:00
43c85b601e game(scripts): fix header comments 2025-02-14 22:12:46 +08:00
4 changed files with 99 additions and 43 deletions

View file

@ -1,8 +1,8 @@
/*
Author: Wai Lam & Reza
Date: 27/1/25
Description: Bathroom interaction
*/
* Author: Wai Lam & Reza
* Date: 27/1/25
* Description: Bathroom interaction
*/
using System.Collections;
using TMPro;

View file

@ -0,0 +1,42 @@
/*
* Author: Wai Lam
* Date: 6/2/25
* Description: UI to follow camera with a slight delay
*/
using UnityEngine;
public class CameraGhostFollow : MonoBehaviour
{
public Transform target; // Usually the Main Camera (XR Rig's Head)
public float followSpeed = 5f; // How quickly it follows the target
public Vector3 offset = new(0, -0.2f, 1.5f); // Position offset
public bool followRotation = true; // Toggle for rotating with the head
public float maxPitchAngle = 30f; // Limits how much the panel tilts up/down
private void LateUpdate()
{
if (!target) return;
// Smooth Position Follow
var desiredPosition = target.position + target.TransformDirection(offset);
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime * followSpeed);
// Smooth Rotation Follow (Yaw + Pitch)
if (!followRotation) return;
// Capture target's full rotation
var targetEulerAngles = target.eulerAngles;
// Handle Pitch (X-axis) - Clamp to avoid extreme tilting
var pitch = targetEulerAngles.x;
if (pitch > 180) pitch -= 360; // Convert to -180 to 180 range
pitch = Mathf.Clamp(pitch, -maxPitchAngle, maxPitchAngle);
// Apply Yaw (Y-axis) and Pitch (X-axis), keep Roll (Z-axis) at 0
var desiredRotation = Quaternion.Euler(pitch, targetEulerAngles.y, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, Time.deltaTime * followSpeed);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 829050d920c070f4783f067cdcb74217
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,42 +1,25 @@
/*
Author : Wai Lam
Date : 10/2/2025
Description : Car obstacle
* Author: Wai Lam
* Date: 10/2/2025
* Description: Car obstacle
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Car : MonoBehaviour
{
public Transform playerRig; // Assign your XR Rig here
public Transform startPoint; // Assign the starting point here
public Transform[] waypoints; // List of waypoints
public float speed = 3.0f; // Speed of movement
public float rotationSpeed = 5.0f; // Smooth turning speed
public Transform playerRig; // Assign your XR Rig here
public Transform startPoint; // Assign the starting point here
private int currentWaypointIndex = 0; // Starting at the first waypoint
public Transform[] waypoints; // List of waypoints
public float speed = 3.0f; // Speed of movement
public float rotationSpeed = 5.0f; // Smooth turning speed
public Vector3 rotationOffset = Vector3.zero;
private void OnTriggerEnter(Collider other)
{
// Check if the collider belongs to the player
if (other.CompareTag("Player"))
{
Debug.Log("Teleporting Player...");
TeleportPlayer();
}
}
private void TeleportPlayer()
{
Vector3 offset = playerRig.position - Camera.main.transform.position;
playerRig.position = startPoint.position + offset;
playerRig.rotation = startPoint.rotation;
}
private int _currentWaypointIndex; // Starting at the first waypoint
void Update()
private void Update()
{
if (waypoints.Length == 0) return;
@ -44,29 +27,49 @@ public class Car : MonoBehaviour
RotateTowardsWaypoint();
}
void MoveTowardsWaypoint()
private void OnTriggerEnter(Collider other)
{
Transform target = waypoints[currentWaypointIndex];
// Check if the collider belongs to the player
if (!other.CompareTag("Player")) return;
Debug.Log("Teleporting Player...");
TeleportPlayer();
}
private void TeleportPlayer()
{
if (Camera.main == null)
{
Debug.LogError("not teleporting player, no main camera found!");
return;
}
var offset = playerRig.position - Camera.main.transform.position;
playerRig.position = startPoint.position + offset;
playerRig.rotation = startPoint.rotation;
}
private void MoveTowardsWaypoint()
{
var target = waypoints[_currentWaypointIndex];
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
// Check if the object has reached the waypoint
if (Vector3.Distance(transform.position, target.position) < 0.1f)
{
// Move to the next waypoint in a loop
currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
}
_currentWaypointIndex = (_currentWaypointIndex + 1) % waypoints.Length;
}
void RotateTowardsWaypoint()
private void RotateTowardsWaypoint()
{
Transform target = waypoints[currentWaypointIndex];
Vector3 direction = (target.position - transform.position).normalized;
var target = waypoints[_currentWaypointIndex];
var direction = (target.position - transform.position).normalized;
if (direction != Vector3.zero)
{
// Apply rotation offset
Quaternion lookRotation = Quaternion.LookRotation(direction) * Quaternion.Euler(rotationOffset);
var lookRotation = Quaternion.LookRotation(direction) * Quaternion.Euler(rotationOffset);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, rotationSpeed * Time.deltaTime);
}
}
}
}