x


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 :(

more ▼

asked May 26 '12 at 09:30 AM

mak gravatar image

mak
64 3 4 6

(comments are locked)
10|3000 characters needed characters left

4 answers: sort oldest

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));
       }
    }
}
more ▼

answered May 26 '12 at 05:19 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 99

A fine answer, thumbs up to you

Jun 04 '12 at 11:07 AM hatchnet
(comments are locked)
10|3000 characters needed characters left

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 ?

more ▼

answered May 26 '12 at 07:44 PM

mak gravatar image

mak
64 3 4 6

Just find the line function ApplyThrottle and totally replace that routine

It's somewhere near line 510

May 26 '12 at 07:46 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

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 ?

more ▼

answered May 26 '12 at 08:27 PM

mak gravatar image

mak
64 3 4 6

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...

May 26 '12 at 08:32 PM whydoidoit

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 :)

May 27 '12 at 04:40 AM mak

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 :(

May 27 '12 at 06:12 AM mak
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 26 '12 at 08:39 PM

mak gravatar image

mak
64 3 4 6

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

May 26 '12 at 08:41 PM whydoidoit

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

May 26 '12 at 08:46 PM whydoidoit

Don't worry about it!

May 26 '12 at 08:47 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x416
x316

asked: May 26 '12 at 09:30 AM

Seen: 845 times

Last Updated: Jun 04 '12 at 11:07 AM