• 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 /
This question was closed Nov 30, 2013 at 04:37 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by 3 · Dec 16, 2012 at 02:48 AM · carmovingracingcarsdriving

How to make car accelerate/decelerate over time

So I'm attempting to make a realistic feeling car. I've got it running, turns feel alright, just how do I make it break appropriately, speed up appropriately, etc etc. I tried, not much luck, but it does stop for a second, go to 8 speed, then go to 30.

 //inspector variables
 
 var speed:float; //speed of the car, tweek as needed based on your scale
 var turnSpeed:float; //turn speed
 var timecar:float;
 var accel: float;
 function Update()
 {
    timecar += Time.deltaTime;
     //grab the input axes
     var steer=Input.GetAxis("Horizontal");
     var gas=Input.GetAxis("Vertical");
 
     //if they're hittin' the gas...
     if (gas!=0)
     {
      InvokeRepeating("addspeed", 1.0, 1.0);
            //take the throttle level (with keyboard, generally +1 if up, -1 if down)
         //  and multiply by speed and the timestep to get the distance moved this frame
         var moveDist=gas*speed*Time.deltaTime;
 
         //now the turn amount, similar drill, just turnSpeed instead of speed
         //   we multiply in gas as well, which properly reverses the steering when going 
         //   backwards, and scales the turn amount with the speed
         var turnAngle=steer * turnSpeed * Time.deltaTime * gas;
 
         //now apply 'em, starting with the turn
         transform.rotation.eulerAngles.y+=turnAngle;
 
         //and now move forward by moveVect
         transform.Translate(Vector3.forward*moveDist);
     }
  }
  function addspeed (){
  if (speed < 30){
  speed++;
  }
  }
 

I've never tried making a racing game in Unity, so I'm completely new to this. I do however have a good amount of experience I Unity as a whole. Thanks for helping!

Just a side-note, the Car Demo provided by Unity won't work, and I didn't want to use it anyways, I prefer making games from scratch.

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

  • Sort: 
avatar image
1
Best Answer

Answer by clunk47 · Dec 16, 2012 at 02:58 AM

Try making an acceleration var float. Like:

var accel : float = 1.0f;

then if(gas) speed+= Input.GetAxis("Vertical") * accel;

Of course this isn't formatted "code", it's Just an example of something you could try...

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 3 · Dec 16, 2012 at 03:53 AM 1
Share

Sorry, do you mean like: var accel: float; if (gas = true) { speed+=Input.GetAxis("Vertical")*accel; }

Doesnt seem to work...

avatar image clunk47 · Dec 16, 2012 at 03:58 AM 0
Share

This isn't a copy and paste script, it is an example you could use to implement into your script. I would write out a script for you but I am about to get some sleep. If you don't figure it out by tomorrow I will work with you more on this :)

avatar image 3 · Dec 16, 2012 at 04:18 AM 2
Share

alright, Yours looks the best honestly, so I'll try to alter yours. If a variation of yours works, Ill give you a check!

Thanks,

avatar image CapitalKage · Feb 12 at 08:36 AM 0
Share

Just in case anyone else reference's this post in the future. I took the advice from @clunk47 and it would look something like this. Of course, you can add a Time adjustment in or even use velocity but, this is a quick way to get the "acceleration/deceleration" going.

float speed; float acceleration = 1f;

if (Input.GetKey(KeyCode.W) && speed <= 30) { transform.Translate(Vector3.forward Time.deltaTime (speed += accel) verticalInput); } else if (Input.GetKey(KeyCode.W) && speed >= 30) { speed = 30; transform.Translate(Vector3.forward Time.deltaTime speed verticalInput); } else { speed = 0; }

avatar image
0

Answer by aburningflame · Dec 16, 2012 at 03:46 AM

Not sure what youre trying to accomplish but if if (Input.GetKeyDown("s")) { speed = 1; while (speed < 16){ speed++; timecar = 0; }

is in an Update function..then if s is pressed speed will always be 16. (Because in 1 tick of update you are looping WHILE speed<16...so that loop will execute 15+ times in 1 update tick - forcing your speed to 16 right away.

You should just do: private int maxSpeed=16; if (Input.GetKeyDown(KeyCode.S)){ speed++; if (speed>maxSpeed) speed=maxSpeed; }else{ speed--; //decelerate speed maybe? }

This allows acceleration over time I suppose.

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 3 · Dec 16, 2012 at 03:50 AM 0
Share

Also doesn't work...

Its not based on time,

What am I doing wrong?

avatar image aburningflame · Dec 21, 2012 at 02:21 AM 0
Share

sorry im not sure i understand. Update() is called every game tick. if S is down, you want to increase your speed. You may need to speed++ every second or so. Im not too sure what youre tryin gto do so its hard to help.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Multiple Cars not working 1 Answer

Mario Kart Style Drifting 0 Answers

How is wheelCollider RPM calculated exactly? 1 Answer

How to add drift to top down car mechanics? 0 Answers

Car exhaust Flame in unity 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