• 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 agrav0 · Jan 15, 2020 at 06:51 AM · unity 2dcar

Can someone give me some guidance for making a drifting car in unity 2d?

I could make a sliding car. but i want the counter steer functionality. My current model of the car has 4 wheels, all of them have a script handling the slip. am currently multiplying the side ways velocity but a value between 0 and 1 (like 0.6 while it's sticky and 0.9 while it's sliding).

in the beginning all my wheels had a mass of 1 so the car was sliding really fast and spinning like crazy. but when i increased the mass to 20 each and now the car slides properly. but the front part of the car doesn't move side ways at all when i counter steer.

This is the code i have right now for each tire

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class wheel : MonoBehaviour
 {
     private Controls controls_i;
     private Rigidbody2D rigid_body;
     private Vector2 current_velocity;
     public bool rotate, power;
     public int rotation_angle, acceleration, max_speed;
     public float max_friction, min_friction, max_slip;
     // Start is called before the first frame update
     void Start()
     {
         controls_i = GameObject.Find("Main_camera").GetComponent<Controls>();
         rigid_body = gameObject.GetComponent<Rigidbody2D>();
     }
     void set_angle(float angle){
         angle = Utils.linear_range(45, -45, rotation_angle, -rotation_angle, angle);
         Vector3 tmp = transform.localEulerAngles;
         tmp.z = angle;
         transform.localEulerAngles = tmp;
     }
     void accelerate(float acceleration){
         if(current_velocity.y < max_speed){
             rigid_body.AddRelativeForce(Vector2.up * acceleration);
         }
     }
     // void apply_sideways_friction(){
     //     float sideways_velocity = current_velocity.x;
     //     float velocity_sign = Mathf.Sign(sideways_velocity);
     //     sideways_velocity = Mathf.Abs(sideways_velocity);
     //     float slip = sideways_velocity - max_friction;
 
     //     float updated_velocity = 0;
     //     if(slip > 0){
     //         slip = sideways_velocity - Utils.curve_map_linear(0, max_slip, min_friction, max_friction, slip, -1);
     //         updated_velocity = slip;
     //     }
 
     //     Vector2 new_velocity = transform.TransformVector(new Vector2(velocity_sign * updated_velocity, current_velocity.y));
     //     rigid_body.velocity = new_velocity;
     // }
 
     public float slip_threshold, stick_threshold;
     public float slip_drag, stick_drag;
     public bool slipping = false;
     void apply_sideways_friction(){
         if(!slipping && (Mathf.Abs(current_velocity.x) >= (slip_threshold * max_speed))){
             slipping = true;
         }else if(slipping && (Mathf.Abs(current_velocity.x) <= (stick_threshold * max_speed))){
             slipping = false;
         }
         float drag = slipping ? slip_drag : stick_drag;
         float new_slip_velocity = current_velocity.x * drag;
         Vector2 new_velocity = transform.TransformVector(new Vector2(new_slip_velocity, current_velocity.y));
         rigid_body.velocity = new_velocity;
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         current_velocity = transform.InverseTransformVector(rigid_body.velocity); 
         Debug.Log("* " + gameObject.name + " | " + transform.InverseTransformVector(rigid_body.velocity));
 
         if(rotate){
             set_angle(controls_i.getSteeringAngle());
         }
 
         apply_sideways_friction();
 
         if(power){
             accelerate(acceleration);
         }
         Debug.DrawLine(transform.position, (Vector2)transform .position + rigid_body.velocity, Color.red, Time.fixedDeltaTime);
         Debug.Log(gameObject.name + " | " + transform.InverseTransformVector(rigid_body.velocity));
     }
 }

Am looking for something like in the game "absolute drift"

I have tried wheel colliders too. But the same problem occurred there, the car slides but couldn't get counter steer.

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

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

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

128 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 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 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 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 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 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

Car AI Avoiding Obstacle 0 Answers

How to create car suspension system? 1 Answer

Getting rid of tire slip 1 Answer

I want to make my car's texture muddy, dynamic 3 Answers

how to get a exciting engine sound for a racing game like CSR, on cell phone?
 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