game(scripts): standardise Car

This commit is contained in:
Mark Joshwel 2025-02-14 22:13:03 +08:00
parent 43c85b601e
commit 3465373560

View file

@ -1,42 +1,25 @@
/* /*
Author : Wai Lam * Author: Wai Lam
Date : 10/2/2025 * Date: 10/2/2025
Description : Car obstacle * Description: Car obstacle
*/ */
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Car : MonoBehaviour public class Car : MonoBehaviour
{ {
public Transform playerRig; // Assign your XR Rig here public Transform playerRig; // Assign your XR Rig here
public Transform startPoint; // Assign the starting point 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
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; 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() private int _currentWaypointIndex; // Starting at the first waypoint
{
Vector3 offset = playerRig.position - Camera.main.transform.position;
playerRig.position = startPoint.position + offset;
playerRig.rotation = startPoint.rotation;
}
void Update()
private void Update()
{ {
if (waypoints.Length == 0) return; if (waypoints.Length == 0) return;
@ -44,29 +27,49 @@ public class Car : MonoBehaviour
RotateTowardsWaypoint(); 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); transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
// Check if the object has reached the waypoint // Check if the object has reached the waypoint
if (Vector3.Distance(transform.position, target.position) < 0.1f) if (Vector3.Distance(transform.position, target.position) < 0.1f)
{
// Move to the next waypoint in a loop // 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]; var target = waypoints[_currentWaypointIndex];
Vector3 direction = (target.position - transform.position).normalized; var direction = (target.position - transform.position).normalized;
if (direction != Vector3.zero) if (direction != Vector3.zero)
{ {
// Apply rotation offset // 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); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, rotationSpeed * Time.deltaTime);
} }
} }
} }