Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Ora · Dec 16, 2013 at 06:49 PM · c#cars

How would i slow a car down when it hit off road on a track

Well i had guesses that i should use something with collision but as far as how to call it into working on script i have no clue. So i was wondering if you guys could give me tips show me how to write it and the meaning of it as i am still very very new to scripting. So yeah any help is greatly appreciated thank you for your time.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nomibuilder · Dec 02, 2014 at 02:09 PM

Here is simple code to do it. Make a duplicate Mesh Collider of Road. Check "is trigger" from inspector view. Add this Script to the road. Adjust it according to your situation.

 #pragma strict
 
 function OnTriggerExit (other : Collider) 
 {
     if(other.gameObject.tag == "Player")
     {
     other.GetComponent.< CarControl>().offroad = true;
     Debug.Log("Off the Road");
     }
 }
 function OnTriggerEnter (other : Collider) 
 {
     if(other.gameObject.tag == "Player")
     {
         other.GetComponent.< CarControl>().offroad = false;
         Debug.Log("back On Track");
     }
 }
 

And in your car Control Script Add this code

 function offtheroad()
 {
     if(offroad)
     {
     if(Speed > offroadlimit){
         Wheel_FL.brakeTorque = 300;
         Wheel_FR.brakeTorque = 300;
     }
     else
     {
         Wheel_FL.brakeTorque = 0;
         Wheel_FR.brakeTorque = 0;
     }
     }
     else
     {
         Wheel_FL.brakeTorque = 0;
         Wheel_FR.brakeTorque = 0;
     }
 }

Comment
Add comment · Show 4 · 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 Tanoshimi2000 · Dec 02, 2014 at 02:15 PM 1
Share

Good Answer! And ultimately close to what I did. Basically, my roads were a separate mesh already. Didn't use the OnTrigger, but did a raycast down where the wheels were, and if it didn't hit Road, added a drag coefficient.

I do believe your idea is much more elegant, and I will probably change the code to match.

avatar image stevethorne · Dec 02, 2014 at 02:21 PM 0
Share

you'll probably want to check the collision with the non-road surface. In this situation the car will only slow down when it's completely off-road. You'll probably want it to slow down even if it's partially off-road as well.

avatar image Nomibuilder · Dec 02, 2014 at 02:49 PM 1
Share

@Tanoshimi2000 Sure :)

avatar image Nomibuilder · Dec 02, 2014 at 02:51 PM 1
Share

@stevethorne Yeah right .. Raycasting is also a good approach

avatar image
0

Answer by MFen · Dec 16, 2013 at 07:13 PM

I would use

 void OnCollisionEnter(Collision other)
 {
     other.getcomponent<vehicleInfo>().speed /= 2;
 }
 void OnCollisionExit (Collision other)
 {
   other.getcomponent<vehicleInfo>().speed *= 2;
 }
     
     //OR
     
 void OnTriggerEnter(Collider other)
 {
 
 }
 
 void OnTriggerExit (Collider other)
 {
 
 }
 

for each of your rough areas add a cube on top of it that is the size and shape, remove the render component, now choose either trigger or collision on the box collider, when the vehicle enters you now set its speed, when it exits revert it back.

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 Tanoshimi2000 · Mar 31, 2014 at 01:14 AM

Here's my code, based upon the Car Tutorial.

 function Check_if_Off_Road()
 {
 //Check to see if we're on the road or dirt
         var rayLeft : Ray;
         var hit : RaycastHit;
         var rayStart : Transform = gameObject.Find("Collider_Bottom").transform;
         rayLeft.origin = rayStart.position;
         rayLeft.direction=-Vector3.up;
         
         offRoad=false;  //default
         if (Physics.Raycast(rayLeft, hit))
         {
             //Play Sound only if moving and not on the road
             if (hit.collider.gameObject.name == "Terrain") {  //On ground, not on road
                 //Play Sound 
                 //sound.OffRoad(true, Mathf.Clamp01(skidGroundSpeed * 0.1));                
                 sound.OffRoad(true, 0.75);
                 //Reduce Speed
                 offRoad=true;
                 dragMultiplier.x = initialDragMultiplierX * handbrakeXDragFactor;
             } else {
                 dragMultiplier.x = initialDragMultiplierX;
                 sound.OffRoad(false, 0);
              } 
         }
 }

Basically, it involves shooting a ray from the bottom of the center of the car. If that hits Terrain then we're off road. Would probably be better to indicate if not Road, but since my roads have custom names, it's easier to identify terrain.

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

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

20 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

Related Questions

Multiple Cars not working 1 Answer

In-Game GUI buttons don't work 2 Answers

How to assign transfrom array in the code? 1 Answer

2d Object floating in space HELP! 1 Answer

(C#) Public variables that have descriptions in the engine. 2 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