• 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
2
Question by CrazyPizza · Mar 15, 2013 at 09:16 AM · carcar gamecar physics

Extremely simple car movement

Hello, I'm trying to make a game like Cubed Rally Racer which has very simple car movement like you can see in the video, I tried to look at the car tutorial and other answers here about cars but I couldn't find what I was looking for.

What I have at the moment is a simple cube, with a rigidbody and 1 of mass(it won't move with more than 1 of mass), and 4 spheres with wheel colliders on them under the cube and this is my movement script:

 function Update () {
 if (Input.GetKey(KeyCode.W))
     rigidbody.AddForce (transform.forward * 100 * Time.deltaTime);
 if (Input.GetKey(KeyCode.S))
     rigidbody.AddForce (-(transform.forward) * 4*Time.deltaTime);
 if (Input.GetKey(KeyCode.D))
     transform.Rotate(0,1.5,0);
 if (Input.GetKey(KeyCode.A))
     transform.Rotate(0,-1.5,0);
 
 
 }

It applies a force to the rigidbody to make it move forward, the "steering" is working but the car flips over as soon as it has some speed and it tries to steer, also the car won't stop moving once it has received some force (it seems like there's no friction). How do I make the car more stable and add friction to make it stop when it stops receiving force?

P.S I found out there's an online version of the game to see how the movement works: CubedRallyRacer

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 Tarlius · Mar 15, 2013 at 09:41 AM 0
Share

Not directly related to your question, but to save you headaches later, you should be applying forces during FixedUpdate() for the physics to be accurate.

As for slowing the car down, you could apply a force in the direction opposite to its velocity when nothing is pressed, although I'm sure there's a better way. Not had to do this myself yet (I want to play with the physics engine :( )

1 Reply

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

Answer by CosgunHalil · Jul 04, 2013 at 01:07 PM

2d movement script I used.

 using UnityEngine;
 using System.Collections;
 
 public class carMove : MonoBehaviour {
     
     float xspeep = 0f;
     float power = 0.001f;
     float friction = 0.95f;
     bool right = false;
     bool left = false;
     
     public float fuel = 2;
     
     
     // Use this for initialization
     void FixedUpdate () {
         
         
         if(right){
             xspeep += power;
             fuel -= power;
         }
         if(left){
             xspeep -= power;
             fuel -= power;
         }
         
         
     }
     
     // Update is called once per frame
     void Update () {
         
         if(Input.GetKeyDown("w")){
             right = true;
         }
         if(Input.GetKeyUp("w")){
             right = false;
         }
         if(Input.GetKeyDown("s")){
             left = true;
         }
         if(Input.GetKeyUp("s")){
             left = false;
         }
         
         if(fuel < 0){
             
             xspeep = 0;
             
         }
         
         xspeep *= friction;
         transform.Translate(Vector3.right * -xspeep);
     
     }
 }
 
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.

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

how to rotate car to camera direction using wheel colliders. 1 Answer

Better RPM?? 0 Answers

transmission clutch help! 1 Answer

Car Gearbox script 1 Answer

simple drift car movement 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