CarCollision
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarCollision : MonoBehaviour {
CarMovement m_Movement;
private void Awake()
{
m_Movement = GetComponent<CarMovement>();
}
private void OnCollisionEnter2D( Collision2D collision )
{
// Debug.Log( "Colliding with " + collision.collider.name + ". Tag: " + collision.collider.tag );
if( collision.collider.CompareTag( "Obstacle" ) == true )
{
m_Movement.OnCollideWithObstacle();
}
}
private void OnTriggerEnter2D( Collider2D otherCollider )
{
if( otherCollider.CompareTag( "Oil" ) == true )
{
m_Movement.OnCollideWithOil();
}
if( otherCollider.CompareTag( "OffCourseArea" ) == true)
{
m_Movement.OnEnterOffCourseArea();
}
}
private void OnTriggerExit2D( Collider2D otherCollider )
{
if( otherCollider.CompareTag( "OffCourseArea" ) == true )
{
m_Movement.OnExitOffCourseArea();
}
}
}
CarInputBase
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent( typeof( CarMovement))]
public class CarInputBase : MonoBehaviour
{
CarMovement m_Movement;
private void Awake()
{
m_Movement = GetComponent<CarMovement>();
}
protected void SetSteeringDirection( float steeringDirection )
{
m_Movement.SetSteeringDirection( steeringDirection);
}
protected void SetEnginePower( float enginePower )
{
m_Movement.SetEnginePower( enginePower );
}
}
CarInputKeyboard
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarInputKeyboard : CarInputBase {
void Update()
{
UpdateSteering();
UpdateEnginePower();
}
void UpdateSteering()
{
SetSteeringDirection( Input.GetAxisRaw( "Horizontal" ) );
}
void UpdateEnginePower()
{
SetEnginePower ( Input.GetAxisRaw( "Vertical" ) );
}
}
CarMovement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarMovement : MonoBehaviour {
public float MaximumEngineForce;
public float MaximumReverseEngineForce;
public float MaximumSteeringTorque;
public float ReversePower;
public float Acceleration;
public float Deceleration;
public Transform VisualsParent;
public float SpinningDuration;
public int SpinningRotations;
float m_EnginePower = 0f;
float m_TargetEnginePower = 0f;
float m_SteeringDirection = 0f;
float m_CurrentMaximumEnginePower = 1f;
bool m_IsSpinning = false;
Rigidbody2D m_Body;
private void Awake()
{
m_Body = GetComponent<Rigidbody2D>();
}
private void Update()
{
UpdateEnginePower();
}
void UpdateEnginePower()
{
float acceleration = Acceleration;
if( m_TargetEnginePower == 0f )
{
acceleration = Deceleration;
}
float targetEnginePower = m_TargetEnginePower * m_CurrentMaximumEnginePower;
m_EnginePower = Mathf.MoveTowards( m_EnginePower, targetEnginePower, acceleration * Time.deltaTime );
if( m_IsSpinning == true )
{
m_EnginePower = 0f;
}
}
void FixedUpdate()
{
ApplyEngineForce();
ApplySteeringForce();
}
void ApplyEngineForce()
{
float maximumEngineForce = MaximumEngineForce;
if( m_EnginePower < 0f )
{
maximumEngineForce = MaximumReverseEngineForce;
}
m_Body.AddForce( transform.up * m_EnginePower * maximumEngineForce, ForceMode2D.Force );
}
void ApplySteeringForce()
{
m_Body.AddTorque( m_SteeringDirection * MaximumSteeringTorque, ForceMode2D.Force );
}
public void SetEnginePower( float enginePower )
{
m_EnginePower = Mathf.Clamp( enginePower, -1f , 1f);
}
public void SetSteeringDirection( float steeringDirection )
{
m_SteeringDirection = Mathf.Clamp( steeringDirection, -1f, 1f);
}
void StartSpinning()
{
if( m_IsSpinning == true )
{
return;
}
StartCoroutine( SpinningRoutine() );
}
IEnumerator SpinningRoutine()
{
m_IsSpinning = true;
float spinningTime = 0f;
while ( spinningTime < SpinningDuration )
{
float spinningProgress = spinningTime / SpinningDuration;
spinningTime += Time.deltaTime;
VisualsParent.transform.localRotation = Quaternion.Euler( 0f, 0f, spinningProgress * SpinningRotations * 360f );
yield return null;
}
VisualsParent.transform.localRotation = Quaternion.identity;
m_IsSpinning = false;
}
public void OnCollideWithObstacle()
{
m_EnginePower = 0f;
}
public void OnCollideWithOil()
{
m_EnginePower = 0f;
StartSpinning();
}
public void OnEnterOffCourseArea()
{
m_CurrentMaximumEnginePower = 0.1f;
}
public void OnExitOffCourseArea()
{
m_CurrentMaximumEnginePower = 1f;
}
}
LapLine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LapLine : MonoBehaviour {
public int Index;
LapManager m_LapManager;
private void Awake()
{
m_LapManager = GetComponentInParent<LapManager>();
}
private void OnTriggerEnter2D ( Collider2D otherCollider )
{
if( otherCollider.CompareTag( "Car" ) == true )
{
m_LapManager.OnLapLinePassed( Index );
}
}
}
LapManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LapManager : MonoBehaviour {
public float CurrentLapTime
{
get
{
if( m_IsLapStarted == false )
{
return 0f;
}
return Time.realtimeSinceStartup - m_CurrentLapStartTime;
}
}
public float LastLapTime { get; private set; }
public float BestLapTime { get; private set; }
bool m_IsLapStarted = false;
float m_CurrentLapStartTime;
int m_LastLapLineIndex = 0;
int m_HighestLapLine;
private void Start()
{
m_HighestLapLine = GetHighestLapLine();
}
int GetHighestLapLine()
{
m_HighestLapLine = 0;
LapLine[] lapLines = GetComponentsInChildren<LapLine>();
for( int i = 0; i < lapLines.Length; ++i )
{
m_HighestLapLine = Mathf.Max(lapLines[ i ].Index);
}
return m_HighestLapLine;
}
public void OnLapLinePassed ( int index )
{
if( index == 0 )
{
if( m_IsLapStarted == false || m_LastLapLineIndex == m_HighestLapLine )
{
OnFinishLinePassed();
}
}
else
{
{
if( index == m_LastLapLineIndex + 1)
{
m_LastLapLineIndex = index;
}
}
}
}
void OnFinishLinePassed()
{
if( m_IsLapStarted == true )
{
LastLapTime = Time.realtimeSinceStartup - m_CurrentLapStartTime;
if( LastLapTime < BestLapTime || BestLapTime == 0f )
{
BestLapTime = LastLapTime;
}
}
m_IsLapStarted = true;
m_CurrentLapStartTime = Time.realtimeSinceStartup;
}
}
LapManagerUI
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent( typeof(LapManager))]
public class LapManagerUI : MonoBehaviour {
public Text LapTimeInfoText;
LapManager m_LapManager;
private void Awake()
{
m_LapManager = GetComponent<LapManager>();
}
void Update()
{
}
void UpdateLapTimeInfoText()
{
LapTimeInfoText.text = "Current: " + SecondsToTime( m_LapManager.CurrentLapTime ) + "\n"
+ "Last: "+ SecondsToTime( m_LapManager.CurrentLapTime ) + "\n"
+ "Best: " + SecondsToTime( m_LapManager.BestLapTime ) + "";
}
string SecondsToTime( float seconds )
{
int displayMinutes = Mathf.FloorToInt( seconds / 60f );
int displaySeconds = Mathf.FloorToInt( seconds % 60f );
int displayFractionSeconds = Mathf.FloorToInt( ( seconds - displaySeconds ) * 100f );
return displayMinutes + ":" + displaySeconds.ToString( "00" ) + ":" + displayFractionSeconds.ToString( "00" );
}
}
SceneSwitch
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneSwitch : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.CompareTag ("Car"))
{
SceneManager.LoadScene (1);
}
}
}
EDIT: My next scene triggers work now.