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,135 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
//script from FREAKINGREX, but minor changes to integrate it proberly
//https://assetstore.unity.com/packages/templates/systems/arcade-car-controller-lite-version-145489
public class CameraControl :MonoBehaviour
{
[SerializeField] KeyCode SetCameraKey = KeyCode.C; //Set next camore on PC hotkey.
[SerializeField] UnityEngine.UI.Button NextCameraButton;
[SerializeField] List<CameraPreset> CamerasPreset = new List<CameraPreset>(); //Camera presets
int ActivePresetIndex = 0;
CameraPreset ActivePreset;
public GameObject TargetCar;
GameController GameController { get { return GameController.Instance; } }
float SqrMinDistance;
Vector3 TargetPoint
{
get
{
if (TargetCar == null)
{
return transform.position;
}
Rigidbody carRB = TargetCar.GetComponent<Rigidbody>();
Vector3 result = carRB.velocity * ActivePreset.VelocityMultiplier*0.05f;
result += TargetCar.transform.position;
result.y = 0;
return result;
}
}
private void Awake() {
CamerasPreset.ForEach (c => c.CameraHolder.SetActive(false));
UpdateActiveCamera ();
if (NextCameraButton)
{
NextCameraButton.onClick.AddListener (SetNextCamera);
}
}
private void FixedUpdate ()
{
if (ActivePreset.EnableRotation && (TargetPoint - transform.position).sqrMagnitude >= SqrMinDistance)
{
Quaternion rotation = Quaternion.LookRotation (TargetPoint - transform.position, Vector3.up);
ActivePreset.CameraHolder.transform.rotation = Quaternion.Lerp (ActivePreset.CameraHolder.transform.rotation, rotation, Time.deltaTime * ActivePreset.SetRotationSpeed);
}
transform.position = Vector3.LerpUnclamped (transform.position, TargetPoint, Time.deltaTime * ActivePreset.SetPositionSpeed);
if (Input.GetKeyDown (SetCameraKey))
{
SetNextCamera ();
}
}
private IEnumerator Start ()
{
while (GameController == null)
{
yield return null;
}
transform.position = TargetPoint;
}
public void SetNextCamera ()
{
ActivePresetIndex = LoopClamp (ActivePresetIndex + 1, 0, CamerasPreset.Count);
UpdateActiveCamera ();
}
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;
}
public void UpdateActiveCamera ()
{
if (ActivePreset != null)
{
ActivePreset.CameraHolder.SetActive(false);
}
ActivePreset = CamerasPreset[ActivePresetIndex];
ActivePreset.CameraHolder.SetActive(true);
SqrMinDistance = ActivePreset.MinDistanceForRotation * 2;
if (ActivePreset.EnableRotation && (TargetPoint - transform.position).sqrMagnitude >= SqrMinDistance)
{
Quaternion rotation = Quaternion.LookRotation (TargetPoint - transform.position, Vector3.up);
ActivePreset.CameraHolder.transform.rotation = rotation;
}
}
[System.Serializable]
class CameraPreset
{
public GameObject CameraHolder;
public float SetPositionSpeed = 1; //Change position speed.
public float VelocityMultiplier; //Velocity of car multiplier.
public bool EnableRotation;
public float MinDistanceForRotation = 0.1f; //Min distance for potation, To avoid uncontrolled rotation.
public float SetRotationSpeed = 1; //Change rotation speed.
}
}

View File

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

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;
}
}

View File

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

View File

@@ -111,11 +111,11 @@ public class carcontrolv2 : MonoBehaviour
}
//idk camera mobning rammer hårdt
float THINP = hInput/10;
if (THINP != hInput)
{
THINP+=hInput/10;
}
// float THINP = hInput/10;
// if (THINP != hInput)
// {
// THINP+=hInput/10;
// }
//lookat.transform.localPosition = new Vector3 (THINP*1.5f,1,4);

View File

@@ -0,0 +1,93 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class gameController1 : MonoBehaviour
{
[SerializeField] KeyCode NextCarKey = KeyCode.N;
[SerializeField] UnityEngine.UI.Button NextCarButton;
public static gameController1 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;
}
}

View File

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