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 Guitarstratdude · Sep 06, 2012 at 08:51 PM · carhowdirectionsaccelerate

How do I make a car accelerate in both directions?

EDIT : I now have the code for accelerating/decelerating In both directions. What I now need to know is how to make "rotateSpeed" 0 if "curSpeed" is 0. I have tried if(curSpeed = 0) { rotateSpeed = 0.0; }

But I get errors. any thoughts?

I have code to accelerate forwards, and decelerate, but this code for accelerating backwards doesn't work. Can anyone help me? Here is the code that doesn't work:

if(Input.GetKey("s")) { curSpeed -= accelerationAmount Time.deltaTime; transform.position += -transform.forward curSpeed * Time.smoothDeltaTime; }

Comment
Add comment · Show 1
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 MickM · Sep 11, 2012 at 05:11 AM 0
Share

For your edit; for an if statement you need to use == ins$$anonymous$$d of =

ie.

if (curSpeed == 0){ rotateSpeed = 0.0; }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by adrenak · Sep 07, 2012 at 07:52 AM

You can always drive a car by changing the location in a mathematical manner as you have done above, however, in my experience, it is kinda analogous to looking for unnecessary trouble, you can get better effects by using the wheel colliders of Unity.

Plus, this tutorial : http://www.gotow.net/andrew/blog/?page_id=78

makes is pretty easy to assemble your own car...and drive away!

This is your first question in here, but don't worry if the very first answer that you get is about some strange WheelCollider thingie... it just takes an hour to follow that tutorial and later on the very same scripts can be perfectly changed according to your will to get any additional results that you want.

I hope this helps!

Vatsal

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 adrenak · Sep 07, 2012 at 07:57 AM 0
Share

I would also recommend staying away from the car control script that the official Unity Car Tutorial contains

avatar image Guitarstratdude · Sep 07, 2012 at 03:21 PM 0
Share

Thanks alot for your help! But if I wanted to do it in a mathematical manner, how would I go about doing that?

avatar image
0

Answer by Strategos · Sep 07, 2012 at 03:53 PM

I would have your controls changing curSpeed as you do currently. so it is either a positive or negative number.

then afterwards have this line

transform.position += ((transform.forward curSpeed) Time.smoothDeltaTime);

That looks like it should work to me.

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 Guitarstratdude · Sep 07, 2012 at 06:50 PM 0
Share

Ok, I tried your code line, but rather than accelerating backwards, curSpeed goes up and when I release the "s" key, the car flies backwards at that speed... Do you want my entire script?

avatar image Strategos · Sep 10, 2012 at 12:13 PM 0
Share

You need to update the position every frame regardless of whether a key is being pressed.

You can slow it down when keys aren't being pressed , either slowly or quickly depending how much inertia you want.

avatar image
0

Answer by SirBedlam · Sep 07, 2012 at 03:44 PM

EDIT

Better yet, try this. It's just for going backward, but it shouldn't be too hard to add forward commands too. :) (You still need a character controller on your car.)

 var curSpeed : float = 2;
 var accelerationAmount : float = 2;
 var maxAcceleration : float = 2;
 var slowDown : float = 1;
 var maxSpeed : float = 20;
 
 function Start () {
 
 }
 
 function Update () {
 
 var controller : CharacterController = GetComponent(CharacterController);
 var backward : Vector3 = transform.TransformDirection(-Vector3.forward);
 
     if (accelerationAmount > maxAcceleration)
     {
         accelerationAmount = maxAcceleration;
     }
 
     if(Input.GetKey("s") && curSpeed < maxSpeed)
     { 
         curSpeed += accelerationAmount * Time.deltaTime;
         controller.SimpleMove(backward * curSpeed);
     }
 
     if(!Input.GetKeyUp("s") && curSpeed > 0)
     { 
         curSpeed -= slowDown * Time.deltaTime;
         controller.SimpleMove(backward * curSpeed);
     }
 }
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

11 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

Related Questions

Multiple Cars not working 1 Answer

audio fades 0 Answers

Unique Car Demo Controls? 1 Answer

use of GuiTextures? 0 Answers

Help with car 2-player Input 0 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