• 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 TheWilsonatorYT · May 08, 2014 at 12:42 PM · carvehicleracingdriving

Mario Kart Style Drifting

Hey guys,

I'm working on a Mario Kart style racing game, and I'm doing the kart controller script. Basically what I have here is really rough but this is what I have so far, I'm going for rigidbody based movement.

What I'm looking for is a little advice on doing this, as I've never programmed a vehicle before and it doesn't 'feel' great, it works, but something just feels wrong (it seems to 'stick' to accelerating for a second before reversing, and vice versa). Also the drifting code is super basic but I'd like to get something very similar to Mario Kart's drifting system.. as I'm a bit new to applying rigidbody forces like this, I just wanted to hear your thoughts on it.

Here's my code:

 void FixedUpdate()
     {
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
 
         isDrifting = Input.GetKey(KeyCode.Space);
 
         if (inputY > 0)
         {
             // If we are going forward
             if (currentSpeed < maxSpeed)
                 currentSpeed += acceleration;
         }
         else if (inputY < 0)
         {
             // If we are going backward
             if (currentSpeed > -maxReverseSpeed)
                 currentSpeed -= decceleration;
         }
         else
         {
             // If we are stopping
             if (currentSpeed > 0)
             {
                 currentSpeed -= decceleration;
             }
             else if (currentSpeed < 0)
             {
                 currentSpeed += acceleration;
             }
         }
 
         if (!isDrifting)
         {
            // Set the drift direction while not drifting (we can't change direction mid-drift!)
            
            if (inputX > 0)
                 driftingDirection = DriftDirection.Right;
             else if (inputX < 0)
                 driftingDirection = DriftDirection.Left;
         }
 
         // Apply currentspeed as rigidbody velocity
         this.rigidbody.velocity += transform.forward * currentSpeed;
 
         // Handle kart drifting
         if (isDrifting)
         {
             if (driftingDirection == DriftDirection.Right)
             {
                 // Drifting right, add some velocity from the left 
                 this.rigidbody.velocity += -transform.right * driftingSpeed;
                 this.rigidbody.AddTorque(transform.up * 1 * steeringAngle * driftingAngle, ForceMode.Acceleration);
             }
             else if (driftingDirection == DriftDirection.Left)
             {
                 // Drifting left, add some velocity from the right
                 this.rigidbody.velocity += transform.right * driftingSpeed;
                 this.rigidbody.AddTorque(transform.up * -1 * steeringAngle * driftingAngle, ForceMode.Acceleration);
             }
         }
 
         if (!isDrifting)
         {
             // Turn normally if not drifting
             this.rigidbody.AddTorque(transform.up * inputX * steeringAngle, ForceMode.Acceleration);
         }
  }

 

I'd really appreciate any advice about how to handle these sort of physics. This is on a rigidbody that is not using gravity by the way, I will be handling my own 'stick to rotation of the road' code to stop the kart from being able to turn upside-down.

Thanks, -Ben

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 ChrisJoosten · May 08, 2014 at 02:14 PM 0
Share

You should take a look at this for moving the "character". http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make car accelerate/decelerate over time 2 Answers

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

Car exhaust Flame in unity 1 Answer

How is wheelCollider RPM calculated exactly? 1 Answer

Realistically drivable car 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