Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mak · May 26, 2012 at 09:30 AM · cartutorial

Car Tutorial Issue ..........

Hi there ,Im begging on my knees now ,all I want is to make the Unity Car Tutorial Function properly ,as in when no keys are touched the car should stop but it doesnt :(

Matthew A said that this piece of code should fix the problem .

There is also a bug in Car.js which I think exacerbates this problem (the car accelerates when the throttle isn't being pressed). I believe the logic there should be something like this instead:

if (throttle == 0) { // Mathf.Sign returns 1 when a value is 0, (also happens in HaveTheSameSign) // this causes inappropriate acceleration unless we check whether throttle is 0 first... :-/ } else if (HaveTheSameSign(relativeVelocity.z, throttle)) { if (!handbrake) { throttleForce = Mathf.Sign(throttle) currentEnginePower rigidbody.mass; } } else { brakeForce = Mathf.Sign(throttle) engineForceValues[0] rigidbody.mass; }

Has anybody any Idea where to put this piece of code ,I am tearing my hair out just trying to get this demo to work ,please please please someone help me out ,I have been trying to sort this out for months but noone seems to know the answer :(

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

5 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by whydoidoit · May 26, 2012 at 05:19 PM

Well as you are desperate I've given it a go.

Looks to me like there isn't much friction going on, not enough to overcome something in the simulation that keeps it rolling. I guess the answer is either to do something with the forwardFriction of the WheelCollider (but Googling implies that there might be problems here) or to apply some level of braking when there is no throttle.

Here's a stab at that:

 function ApplyThrottle(canDrive : boolean, relativeVelocity : Vector3)
 {
     if(canDrive)
     {
         var throttleForce : float = 0;
         var brakeForce : float =  0;
         
         if(rigidbody.velocity.magnitude < 0.01 && throttle == 0) {
             rigidbody.velocity = Vector3.zero;
         } else {
         
             if(relativeVelocity.z > 0.00001 && throttle == 0)
             {
                 brakeForce = -21000 * relativeVelocity.z - 2000;
             }
             
             if(relativeVelocity.z < 0 && throttle ==0)
             {
                 brakeForce = 20000 * relativeVelocity.z;
             }
             
             
             if(throttle != 0)
             {
                 if (HaveTheSameSign(relativeVelocity.z, throttle))
                 {
                     if (!handbrake)
                         throttleForce = Mathf.Sign(throttle) * currentEnginePower * rigidbody.mass;
                 }
                 else
                     brakeForce = Mathf.Sign(throttle) * engineForceValues[0] * rigidbody.mass;
                 
             }
             rigidbody.AddForce(transform.forward * Time.deltaTime * (throttleForce + brakeForce));
         }
     }
 }
Comment
Add comment · Show 2 · 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 hatchnet · Jun 04, 2012 at 11:07 AM 0
Share

A fine answer, thumbs up to you

avatar image ultimatearcade · Oct 03, 2013 at 04:38 AM 0
Share

this is AWESO$$anonymous$$E! the autodriving was "driving" me crAzYY!!!! :)

one correction... when i applied this great script i had to also add a "-" in front of 20000 for me to get the car not to also auto drive backwards as well... hope this helps whoever else is battling this issue!

if(relativeVelocity.z < 0 && throttle ==0) { brakeForce = -20000 * relativeVelocity.z; }

avatar image
0

Answer by mak · May 26, 2012 at 07:44 PM

Hey there Mike thanks for the reply :) can you tell me where I drop this into car.js please mate,I have tried different places but keep getting this error ?

Assets/Standard Assets/Scripts/Car.js(595,10): BCE0089: Type 'Car' already has a definition for 'ApplyThrottle(boolean, UnityEngine.Vector3)'.

ps im writing for Android if that makes any difference ?

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 whydoidoit · May 26, 2012 at 07:46 PM 1
Share

Just find the line function ApplyThrottle and totally replace that routine

It's somewhere near line 510

avatar image
0

Answer by mak · May 26, 2012 at 08:27 PM

Im redownloading the untouched car tut and ill get right back to you :) ,fingers crossed :)

silly question im on latest unity 3.5etc would that make any difference ?

Comment
Add comment · Show 3 · 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 whydoidoit · May 26, 2012 at 08:32 PM 0
Share

Just a note: you shouldn't post answers unless you are answering things :) Just use the comments - it messes up your accepted score too!

Well I'm running 3.5.2 and that code made the car stop for me :) You can adjust those 20000 numbers to have less of an effect/more of an effect etc. I'm not sure this is the ideal way to have it work - the car model is doing stuff like Engine braking etc, but I couldn't make it stop that way...

avatar image mak · May 27, 2012 at 04:40 AM 0
Share

lol ,have been all over the place with answers instaed of comments lol :P ,but once again thank you :) this was literally driving me nuts lol :)

avatar image mak · May 27, 2012 at 06:12 AM 0
Share

Android ^^ ,it was trying to make the game run on Android/I-phone all along ,I feel like such a fool now lol ,ah well back to the drawing board I guess :p . Basically when I switch platform for Android the car rolls ... Im beginning to think its all just not possible :(

avatar image
0

Answer by mak · May 26, 2012 at 08:39 PM

Woot ,dont take this the wrong way but I LOVE YOU !!!!!!!!!!!!!!!!!! Absolutely perfect THANK YOU !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Comment
Add comment · Show 3 · 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 whydoidoit · May 26, 2012 at 08:41 PM 0
Share

Just flag is as answerer and I'll be happy :)

avatar image whydoidoit · May 26, 2012 at 08:46 PM 2
Share

Perferably flag me as answering it not yourself ;) !!!!!

avatar image whydoidoit · May 26, 2012 at 08:47 PM 1
Share

Don't worry about it!

avatar image
0

Answer by kockaart · Apr 06, 2014 at 03:21 PM

check this out, sliding problem is solved as well:

function ApplyThrottle(canDrive : boolean, relativeVelocity : Vector3) { if(canDrive) { var throttleForce : float = 0; var brakeForce : float = 0;

    if(rigidbody.velocity.magnitude < 0.3 && throttle == 0) {
      rigidbody.velocity = Vector3.zero;
      rigidbody.constraints = RigidbodyConstraints.FreezeAll;
    } else {
       rigidbody.constraints = RigidbodyConstraints.None;
      if(relativeVelocity.z > 0.00001 && throttle == 0)
      {
       brakeForce = -21000 * relativeVelocity.z - 2000;
      }
 
      if(relativeVelocity.z < 0 && throttle ==0)
      {
       brakeForce = -20000 * relativeVelocity.z;
      }
 
 
      if(throttle != 0)
      {
       rigidbody.constraints = RigidbodyConstraints.None;
       if (HaveTheSameSign(relativeVelocity.z, throttle))
       {
           if (!handbrake)
              throttleForce = Mathf.Sign(throttle) * currentEnginePower * rigidbody.mass;
       }
       else
           brakeForce = Mathf.Sign(throttle) * engineForceValues[0] * rigidbody.mass;
 
      }
      rigidbody.AddForce(transform.forward * Time.deltaTime * (throttleForce + brakeForce));
    }
 }

}

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

7 People are following this question.

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

Related Questions

Stuck in Car tutorial - Wheels 0 Answers

Car movement script ? 1 Answer

need tutorials for rigidbody.Addforce 1 Answer

Add Checkpoints and Laps Unity Car Tutorial 5 Answers

How do I download the new unity 5 car tutor? 1 Answer


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