Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by timvderduinen · May 31, 2019 at 10:24 PM · scripting problemscript.scripting beginnerscriptingproblem

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





Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Jun 01, 2019 at 07:09 PM 0
Share

Put a debug in your trigger exit and see if its getting called at all.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image timvderduinen · Jun 04, 2019 at 03:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

265 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges