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 /
avatar image
1
Question by Crazy88 · Oct 05, 2011 at 06:40 PM · rigidbodycaracceleration

Car Accelerate Over Time. (JavaScript and RigidBody)

Hello. This is my first post so please be gentle. I am working with Unity's Car Tutorialand trying to make the car accelerate by itself. I disabled the throttle and break functions in Car.js so the player can only steer left or right. My problem is creating a function that will make the car accelerate until it reaches the maxSpeed. Currently, the car will slowly move forward and at the same speed. I am also unsure if I need to call the function in Update() or FixedUpdate(). Any help will be greatly appreciated.

 var curSpeed : float = 0.0; // holds current speed value
 var acc : float = 10.0; // rate car accelerates at
 var maxSpeed : float = 160.0; // max speed the car can go
 
 function FixedUpdate()
 {    
     // The rigidbody velocity is always given in world space, but in order to work in local space of the car model we need to transform it first.
     var relativeVelocity : Vector3 = transform.InverseTransformDirection(rigidbody.velocity);
     
     CalculateState();    
     
     UpdateFriction(relativeVelocity);
     
     UpdateDrag(relativeVelocity);
         
         ApplyAcceleration(relativeVelocity);
     
     ApplySteering(canSteer, relativeVelocity);
 }
 
 function ApplyAcceleration(relativeVelocity)
 {
     curSpeed += acc;
     curSpeed = Mathf.Clamp(curSpeed,-maxSpeed,maxSpeed); // Clamps curSpeed
     rigidbody.AddForce(transform.forward * curSpeed * Time.deltaTime);
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Crazy88 · Oct 07, 2011 at 08:23 AM

Thanks for your help. That link explained the Update/FixedUpdate much better then my teacher. Also, that if statement seems obvious now.

But my car still doesn't accelerate the way I want it too. My function uses three; variables; speed, topSpeed, and accRate.

I want the car to start at a speed of 0 and begin to accelerate at accRate until it reaches topSpeed. Right now my function looks like this:

 function ApplyAcceleration(relativeVelocity)
 {
     if (speed < maxSpeed) 
     {
         speed += accRate;
         speed = Mathf.Clamp(speed,maxSpeed,Time.timeDelta); // Clamps current Speed
        
     }
     rigidbody.AddForce(transform.forward * speed);
    
 }

The car is still moving very slow and I don't see any acceleration.

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 Zero_Zagarth · Feb 18, 2013 at 03:31 PM 0
Share

Use Force$$anonymous$$ode.Acceleration

avatar image
0

Answer by Meltdown · Oct 05, 2011 at 06:59 PM

Update your FixedUpdate function with this line...

 if(curSpeed < maxSpeed)
     ApplyAcceleration(relativeVelocity);

So the accelleration will be applied until the maxspeed has been reached.

Read this previous answer for an understanding of when to use Update and FixedUpdate.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Car is not moving Straight with Acceleration Button Pressed 2 Answers

can't slow my car down 2 Answers

Car dynamics? 0 Answers

How to make car land on 4 wheels and maintain speed? 0 Answers

How to un-parent and add rigidbody on collision? 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