Professional Documents
Culture Documents
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.SceneManagement;
[Serializable]
public struct Wheel
{
public GameObject wheelModel;
public WheelCollider wheelCollider;
public Axel axel;
}
//Camera
private CameraFollow cameraFollow;
[HideInInspector]
public float isAccelerating = 0;
[HideInInspector]
public float Gear = 1;
[HideInInspector]
public bool isChangingGear = true;
[HideInInspector]
public int isBraking = 0;
//Indicators
public GameObject LeftIndicator;
public GameObject RightIndicator;
int typecontrol;
//Speed
private Vector3 lastPosition;
private float speed;
public Text CarSpeed;
public Text GearText;
//Sounds of Car
public AudioClip Idle_Sound;
public AudioClip Accelerating_Sound;
void Start()
{
audiosource = gameObject.AddComponent<AudioSource>();
PlayIdleSound();
leftIndicatorToggle.onValueChanged.AddListener(OnLeftIndicatorToggleChange);
rightIndicatorToggle.onValueChanged.AddListener(OnRightIndicatorToggleChange);
//---------------------------------------------------------
}
private void GetCarSpec()
{
string carName = PlayerPrefs.GetString(PlayerPrefsKeys.CAR_NAME, "Honda");
maxAcceleration = PlayerPrefs.GetInt(carName +
PlayerPrefsKeys.CAR_ACCELERATION, 20);
brakeAcceleration = PlayerPrefs.GetInt(carName +
PlayerPrefsKeys.CAR_BRAKING, 40);
turnSensitivity = PlayerPrefs.GetFloat(carName +
PlayerPrefsKeys.CAR_HANDLING, 1);
maxSteerAngle = PlayerPrefs.GetInt(carName +
PlayerPrefsKeys.CAR_STEERANGLE, 30);
speedLimit = PlayerPrefs.GetInt(carName + PlayerPrefsKeys.CAR_SPPED, 80);
}
void Update()
{
if(PauseMenu.isGamePaused || CompletionMenu.isCompleted)
{
Time.timeScale = 0;
return;
}
isCarNotMoving = IsCarStopped();
GetControlType();
AnimateWheels();
isMoving();
Steer();
Brake();
if(!CarAudio)
{
audiosource.Pause();
}
else
{
audiosource.UnPause();
}
UpdateSounds();
}
private void FixedUpdate()
{
if (PauseMenu.isGamePaused || CompletionMenu.isCompleted)
{
return;
}
GetDistanceUnit();
}
private void GetControlType()
{
typecontrol = PlayerPrefs.GetInt(PlayerPrefsKeys.CONTROL_MODE_KEY,0);
if (typecontrol == 0)
{
control = ControlMode.Buttons;
}
else if (typecontrol == 1)
{
control = ControlMode.Tilt;
}
GetInputs();
}
void GetInputs()
{
if (control == ControlMode.Keyboard)
{
moveInput = Input.GetAxis("Vertical");
steerInput = Input.GetAxis("Horizontal");
}
else if (control == ControlMode.Tilt)
{
SteerButtons.SetActive(false);
// Use tilt input for steering
steerInput = Input.acceleration.x * turnSensitivity;
}
else if (control == ControlMode.Buttons)
{
SteerButtons.SetActive(true);
}
}
void Move()
{
//Removing Decelaration
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 0;
}
void Steer()
{
foreach (var wheel in wheels)
{
if (wheel.axel == Axel.Front)
{
if (control == ControlMode.Tilt)
{
// Adjust steering based on tilt input
var tiltSteer = Input.acceleration.x * turnSensitivity *
maxSteerAngle;
wheel.wheelCollider.steerAngle =
Mathf.Lerp(wheel.wheelCollider.steerAngle, tiltSteer, 0.6f);
}
else
{
// Use regular input for steering
var keyboardSteer = steerInput * turnSensitivity *
maxSteerAngle;
wheel.wheelCollider.steerAngle =
Mathf.Lerp(wheel.wheelCollider.steerAngle, keyboardSteer, 0.6f);
}
}
}
}
}
else
{
BackLights.SetActive(false);
foreach (var wheel in wheels)
{
wheel.wheelCollider.brakeTorque = 0;
}
}
}
void AnimateWheels()
{
foreach (var wheel in wheels)
{
Quaternion rot;
Vector3 pos;
wheel.wheelCollider.GetWorldPose(out pos, out rot);
wheel.wheelModel.transform.position = pos;
wheel.wheelModel.transform.rotation = rot;
}
}
//Stopping Deceleration
foreach (var wheel in wheels)
{
wheel.wheelCollider.attachedRigidbody.drag = 0f;
}
Move();
isMovingForward = true;
}
else if (isAccelerating != 1)
{
//Decelarating
foreach (var wheel in wheels)
{
wheel.wheelCollider.motorTorque = 0;
wheel.wheelCollider.attachedRigidbody.drag = 0.1f;
}
BackLights.SetActive(true);
isMovingForward = false;
}
}
public void ChangeGear()
{
if (Gear == 1)
{
isGearDrive = false;
Gear = -1;
GearText.text = "Gear\nR";
}
else if (Gear == -1)
{
isGearDrive = true;
Gear = 1;
GearText.text = "Gear\nD";
}
}
public void Accelerating(int input)
{
isAccelerating = input;
}
public void ToggleBraking(int brake)
{
isBraking = brake;
}
if (value)
{
// If left indicator is turned on, turn off the right indicator
rightIndicatorToggle.isOn = false;
StartBlinking(LeftIndicator);
}
else
{
StopBlinking();
LeftIndicator.SetActive(false); // Turn off the lights immediately when
indicator is turned off
}
}
if (value)
{
// If right indicator is turned on, turn off the left indicator
leftIndicatorToggle.isOn = false;
StartBlinking(RightIndicator);
}
else
{
StopBlinking();
RightIndicator.SetActive(false); // Turn off the lights immediately
when indicator is turned off
}
}
void StopBlinking()
{
if (blinkCoroutine != null)
{
// Stop the ongoing blinking coroutine
StopCoroutine(blinkCoroutine);
blinkCoroutine = null;
}
}
void SetIndicatorLights()
{
LeftIndicator.SetActive(leftIndicatorActive);
RightIndicator.SetActive(rightIndicatorActive);
}
public bool IsCarStopped() //To check whether the car is stopped on not
{
//Debug.Log(carRb.velocity.magnitude);
return carRb.velocity.magnitude < 0.1f;
}
public void setCarSpeed(float speed) //Set the car speed for car Discovery time
{
maxAcceleration = speed;
}
void CalculateSpeed()
{
Vector3 currentPosition = transform.position;
float distanceTraveled = Vector3.Distance(currentPosition, lastPosition);
speed = distanceTraveled / Time.deltaTime;
if (speedUnit == SpeedUnit.KilometersPerHour)
{
CarSpeed.text = intspeed + "\nkm/h";
}
else if (speedUnit == SpeedUnit.MilesPerHour)
{
CarSpeed.text = intspeed + "\nmp/h";
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Boundary"))
{
if (scoreManager != null)
{
scoreManager.HandleCollisionWithBoundary();
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Complete"))
{
CompletionMenu.isCompleted = true;
scoreManager.LevelTime(levelTimeStart);
}
}
//Sounds
void UpdateSounds()
{
// Check if the car is accelerating and play the accelerating sound
if (isAccelerating == 1)
{
if (!audiosource.isPlaying || audiosource.clip != Accelerating_Sound)
{
PlayAcceleratingSound();
}
// Check if there's a gear change and play the gear change sound
if (isChangingGear)
{
isChangingGear = false; // Reset the flag
}
}
void PlayIdleSound()
{
audiosource.clip = Idle_Sound;
audiosource.loop = true;
audiosource.pitch = 1f; // Reset pitch to default
audiosource.Play();
}
void PlayAcceleratingSound()
{
audiosource.clip = Accelerating_Sound;
audiosource.volume = 0.25f;
audiosource.loop = true;
audiosource.pitch = minPitch; // Set initial pitch to minPitch
audiosource.Play();
}