UnityCar - Controlling Speed with a duration?
Hi, I am using the UnityCar(s) for my Racing Game. I'm working on some twists, one of them being the Speed increase when going through a collider. As of now, the default speed of these cars is put to:
[SerializeField] public static float m_Topspeed = 20;
When a Car hits a collider (3D Cube as an example), the Car will gain speed set by the multiplier. Now, I can't seem to find a solution to this on regulating how long this duration is and when this boost stops. I thought that OnTriggerExit would stop this speed increase but for some reason, the boost starts from that point on.
Here is my script, how would I fix this issue?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Vehicles.Car;
public class SpeedIncrease : MonoBehaviour
{
public float multiplier = 0.1f;
public GameObject DriveThroughEffect;
public AudioSource BoostSound;
public GameObject Car;
public GameObject AI01;
public GameObject AI02;
void OnTriggerEnter(Collider other)
{
Instantiate(DriveThroughEffect, transform.position, transform.rotation);
BoostSound.Play();
CarController.m_Topspeed *= multiplier;
Debug.Log("ReceiveBoost");
}
void OnTriggerExit(Collider other)
{
CarController.m_Topspeed /= multiplier;
}
}
Put a debug in your trigger exit and see if its getting called at all.
Answer by timvderduinen · Jun 02, 2019 at 12:35 PM
Hey, @Lo0NuhtiK thanks for your reply, its being called but i think the car prefab is holding it back. I get this error message;
NullReferenceException: Object reference not set to an instance of an object UnityStandardAssets.Vehicles.Car.WheelEffects.EmitTyreSmoke () (at Assets/Standard Assets/Vehicles/Car/Scripts/WheelEffects.cs:47) UnityStandardAssets.Vehicles.Car.CarController.CheckForWheelSpin () (at Assets/Standard Assets/Vehicles/Car/Scripts/CarController.cs:282) UnityStandardAssets.Vehicles.Car.CarController.Move (System.Single steering, System.Single accel, System.Single footbrake, System.Single handbrake) (at Assets/Standard Assets/Vehicles/Car/Scripts/CarController.cs:170)
I've deleted the particle effects as it was causing lag with multiple AI's being around. I think increasing CheckForWheelSpin should do the trick. Any other ideas are welcome.
Answer by jijigri · Jun 02, 2019 at 02:54 PM
This may be too simple to be right, but couldn't you use a Coroutine, or even an Invoke function for this?
//When you enter the trigger
StartCoroutine("SpeedBoostDuration");
//Create a function
IENumerator SpeedBoostDuration() {
yield return new WaitForSeconds(timeInSeconds); //timeInSeconds might be a public float
CarController.m_Topspeed /= multiplier;
}
I also think you should make an initial speed variable, and put the speed back to the initial speed, instead of dividing it by the multiplier, but it's completely up to you.
Hey @jijigri thanks for your help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Vehicles.Car;
public class SpeedIncrease : $$anonymous$$onoBehaviour
{
//public GameObject DriveThroughEffect;
public AudioSource BoostSound;
void OnTriggerEnter(Collider other)
{
//Instantiate(DriveThroughEffect, transform.position, transform.rotation);
BoostSound.Play();
CarController.m_Topspeed = 400;
StartCoroutine("SpeedBoostDuration");
Debug.Log("ReceiveBoost");
CarController.m_Topspeed = 80;
Debug.Log("BoostOver");
}
IEnumerator SpeedBoostDuration()
{
yield return new WaitForSeconds(3);
}
void OnTriggerExit(Collider other)
{
CarController.m_Topspeed = 80;
Debug.Log("BoostOver");
}
}
This is what I got right now, the ontriggerexit gets called as soon as I hit the collider. I've tried increasing 3D object and its collider but that doesnt seem to fix the problem.
Your answer
Follow this Question
Related Questions
Set public gameobject by raycast hit target 1 Answer
Flashlight flickering script? 0 Answers
1st Person shooter, move player forward where cam is facing 1 Answer
CAMERAS ERROR , I NEED HELP 0 Answers
How do i make an uppercut attack? 0 Answers