This commit is contained in:
Crizomb 2024-12-10 22:32:20 +01:00
parent 3a7ce962e7
commit 058e61a4fd
38 changed files with 3915 additions and 363 deletions

View file

@ -2,8 +2,12 @@ using UnityEngine;
public class Boost : MonoBehaviour
{
[SerializeField] Rigidbody rb;
[SerializeField] float boostMultiplier;
[SerializeField] Rigidbody _rb;
[SerializeField] float boostForce;
[SerializeField] float boostRefillTime;
[HideInInspector]
public float charge { get; private set; } // Beetwen 0 and 1
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
@ -14,10 +18,13 @@ public class Boost : MonoBehaviour
// Update is called once per frame
void Update()
{
// Middle click
if (Input.GetMouseButton(2))
charge = Mathf.Clamp(charge + Time.deltaTime / boostRefillTime, 0, 1.0f);
// Middle click and 0.99f because I'm affraid of floating point comparison
if (Input.GetMouseButton(2) && charge >= 0.99f)
{
rb.linearVelocity *= boostMultiplier;
_rb.AddForce(boostForce * _rb.transform.forward, ForceMode.Impulse);
charge = 0;
}
}
}

View file

@ -0,0 +1,37 @@
using UnityEngine;
public class CheckpointCheck : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public const int MAX_LAPS = 3;
[HideInInspector] public int NumberOfLaps { get; private set; } = 0;
private int currentCheckPointIndex = 0;
private int numberOfCheckpoints;
void Start()
{
numberOfCheckpoints = Object.FindObjectsByType<Checkpoint>(FindObjectsSortMode.None).Length;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider collision)
{
print("Collision");
if (!collision.gameObject.TryGetComponent(out Checkpoint checkpoint)) return;
if (checkpoint.Index == currentCheckPointIndex) currentCheckPointIndex++;
if (currentCheckPointIndex == numberOfCheckpoints)
{
currentCheckPointIndex = 0;
NumberOfLaps++;
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3ccd479800bf10341b6e8ffa6bff8e33

View file

@ -0,0 +1,50 @@
using UnityEngine;
public class Drift : MonoBehaviour
{
[SerializeField] Rigidbody rb;
[SerializeField] private float baseLateralFrictionCoeff = 5f;
[SerializeField] private float lateralVelocityStartDriftThreshold = 10f;
[SerializeField] private float lateralVelocityTotalDriftThreshold = 15f;
[SerializeField] private TrailRenderer trailRendererLeft;
[SerializeField] private TrailRenderer trailRendererRight;
[SerializeField] RotateWithMouse moreRotateWhenDrift;
[SerializeField] [Range(1, 5)] float RotateLerpSpeedMultipler;
private float lateralFrictionCoeff;
private float baseRotateLerpSpeed;
private float newRotateLerpSpeed => baseRotateLerpSpeed * RotateLerpSpeedMultipler;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
baseRotateLerpSpeed = moreRotateWhenDrift.lerpSpeed;
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 forwardVelocity = Vector3.Project(rb.linearVelocity, transform.forward);
Vector3 lateralVelocity = rb.linearVelocity - forwardVelocity;
// dynamicCoeff is equal to 1 when no drift is happening, and 0 when the drift is at its maximum
float dynamicCoeff = Mathf.InverseLerp(lateralVelocityTotalDriftThreshold, lateralVelocityStartDriftThreshold, lateralVelocity.magnitude);
trailRendererLeft.emitting = dynamicCoeff < 0.9f;
trailRendererRight.emitting = dynamicCoeff < 0.9f;
// When no drift -> lerpSpeed = baseRotateLerpSpeed, when drift -> lerpSpeed = newRotateLerpSpeed
moreRotateWhenDrift.lerpSpeed = Mathf.Lerp(newRotateLerpSpeed, baseRotateLerpSpeed, dynamicCoeff);
lateralFrictionCoeff = baseLateralFrictionCoeff * dynamicCoeff;
print(dynamicCoeff);
Vector3 lateralFrictionForce = -lateralVelocity * lateralFrictionCoeff;
rb.AddForce(lateralFrictionForce, ForceMode.Acceleration);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 126ace550d661844b89827d37837767f

View file

@ -4,9 +4,9 @@ public class RotateWithMouse : MonoBehaviour
{
[SerializeField] LayerMask layerToIgnore;
[SerializeField] Transform targetTransform;
[SerializeField]
[Range(0f, 1f)]
float lerpSpeed = 0.1f;
public float lerpSpeed = 0.01f;
Rigidbody rb;
Camera cam;

View file

@ -0,0 +1,7 @@
using UnityEngine;
public class Checkpoint : MonoBehaviour
{
[SerializeField] int index;
[HideInInspector] public int Index => index;
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 49b8fecd5d0962549bfe72e61625b507

View file

@ -0,0 +1,9 @@
using UnityEngine;
using UnityEngine.Events;
public class EventBus
{
public static UnityEvent<float> WinEvent = new UnityEvent<float>();
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 98c1de9f5caed7c46b5ad76934892b27

View file

@ -16,4 +16,9 @@ public class Rocket : MonoBehaviour
{
}
private void OnCollisionEnter(Collision collision)
{
Destroy(gameObject);
}
}

View file

@ -1,26 +0,0 @@
using TMPro;
using UnityEngine;
public class UIManager : MonoBehaviour
{
[SerializeField] TextMeshProUGUI timeText;
[SerializeField] TextMeshProUGUI velocityText;
[SerializeField] Rigidbody car;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
int minute = (int) Time.time / 60;
int secondes = (int) Time.time % 60;
timeText.text = $"Time : {minute} min {secondes}s";
int velocity = (int) car.linearVelocity.magnitude;
velocityText.text = $"{velocity} m/s";
}
}

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7b3efa9ea1ea7764d885075077873038
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,54 @@
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.UIElements;
using UImage = UnityEngine.UI.Image;
public class UIManager : MonoBehaviour
{
[SerializeField] TextMeshProUGUI timeText;
[SerializeField] TextMeshProUGUI velocityText;
[SerializeField] TextMeshProUGUI lapText;
[SerializeField] UImage boostBar;
[SerializeField] Rigidbody car;
[SerializeField] Boost boost;
[SerializeField] CheckpointCheck checkpointCheck;
bool has_won = false;
float time = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
int minute = (int) time / 60;
int secondes = (int) time % 60;
timeText.text = $"Time : {minute} min {secondes}s";
int velocity = (int) car.linearVelocity.magnitude;
velocityText.text = $"{velocity} m/s";
boostBar.fillAmount = boost.charge;
lapText.text = $"Lap : {checkpointCheck.NumberOfLaps + 1} / {CheckpointCheck.MAX_LAPS}";
if (checkpointCheck.NumberOfLaps == CheckpointCheck.MAX_LAPS && !has_won)
{
Win();
}
}
void Win()
{
has_won = true;
EventBus.WinEvent.Invoke(time);
}
}

View file

@ -0,0 +1,30 @@
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
public class WinManager : MonoBehaviour
{
[SerializeField] Canvas winCanvas;
[SerializeField] Canvas uiCanvas;
[SerializeField] TextMeshProUGUI scoreText;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
EventBus.WinEvent.AddListener(OnWin);
}
void OnWin(float time)
{
uiCanvas.gameObject.SetActive(false);
winCanvas.gameObject.SetActive(true);
int minute = (int)time / 60;
int secondes = (int)time % 60;
scoreText.text = $"Time : {minute} min {secondes}s";
}
public void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5f3047ee8cef5064bbe5a10822558645