Crazy camera

This commit is contained in:
magn9775
2024-04-23 14:43:20 +02:00
parent 2990a061ae
commit 75d677442d
14 changed files with 1037 additions and 926 deletions

View File

@@ -0,0 +1,97 @@
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Base class game controller.
/// </summary>
public class GameController :MonoBehaviour
{
[SerializeField] KeyCode NextCarKey = KeyCode.N;
[SerializeField] UnityEngine.UI.Button NextCarButton;
public static GameController Instance;
public GameObject PlayerCar;
public static bool RaceIsStarted { get { return true; } }
public static bool RaceIsEnded { get { return false; } }
carcontrolv2 m_PlayerCar;
List<carcontrolv2> Cars = new List<carcontrolv2>();
int CurrentCarIndex = 0;
protected virtual void Awake ()
{
Instance = this;
//Find all cars in current game.
Cars.AddRange (GameObject.FindObjectsOfType<carcontrolv2> ());
Cars = Cars.OrderBy (c => c.name).ToList();
foreach (var car in Cars)
{
var userControl = car.GetComponent<carcontrolv2>();
var audioListener = car.GetComponent<AudioListener>();
if (userControl == null)
{
userControl = car.gameObject.AddComponent<carcontrolv2> ();
}
if (audioListener == null)
{
audioListener = car.gameObject.AddComponent<AudioListener> ();
}
userControl.enabled = false;
audioListener.enabled = false;
}
m_PlayerCar = Cars[0];
m_PlayerCar.GetComponent<carcontrolv2> ().enabled = true;
m_PlayerCar.GetComponent<AudioListener> ().enabled = true;
if (NextCarButton)
{
NextCarButton.onClick.AddListener (NextCar);
}
}
void Update ()
{
if (Input.GetKeyDown (NextCarKey))
{
NextCar ();
}
}
private void NextCar ()
{
m_PlayerCar.GetComponent<carcontrolv2> ().enabled = false;
m_PlayerCar.GetComponent<AudioListener> ().enabled = false;
CurrentCarIndex = LoopClamp (CurrentCarIndex + 1, 0, Cars.Count);
m_PlayerCar = Cars[CurrentCarIndex];
m_PlayerCar.GetComponent<carcontrolv2> ().enabled = true;
m_PlayerCar.GetComponent<AudioListener> ().enabled = true;
}
public static int LoopClamp (int value, int minValue, int maxValue)
{
while (value < minValue || value >= maxValue)
{
if (value < minValue)
{
value += maxValue - minValue;
}
else if (value >= maxValue)
{
value -= maxValue - minValue;
}
}
return value;
}
}