Search Unity

[Beginner] Arcady Kart drift Help!

Discussion in 'Scripting' started by MatheusCohen, Sep 17, 2017.

  1. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    Hi,

    i'm on a Kart Racing project(as many out there, using mario kart as a basis), and after reading a bunch around here, i decided to not use the wheelcolliders and more realistic stuff.

    i tried looking for Youtube tutorials, but none were similar to what i wanted.

    Right now my code for the Kart control is like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CarDrive : MonoBehaviour {
    6.  
    7.     private Rigidbody car;
    8.     public Transform wheels;
    9.     public Transform turn;
    10.     public Transform Center;
    11.  
    12.     public float Speed;
    13.     public float RotationSpeedWheels;
    14.     public float RotationSpeedCar;
    15.     public float DriftForce;
    16.     public float Y;
    17.     public float X;
    18.     public float Rotation;
    19.  
    20.     public bool Drift;
    21.  
    22.  
    23.  
    24.     void Start () {
    25.         car = GetComponent<Rigidbody>();
    26.  
    27.     }
    28.  
    29.     void Update () {
    30.         X = Input.GetAxis("Horizontal");
    31.         Y = Input.GetAxis("Vertical");
    32.  
    33.         car.velocity = transform.right * Y * Speed;
    34.  
    35.         wheels.rotation = Quaternion.Slerp(wheels.rotation, Quaternion.Euler(0, car.rotation.eulerAngles.y + X * Rotation, 0), Time.deltaTime * RotationSpeedWheels);
    36.  
    37.         if (car.velocity != Vector3.zero && Y>0 && Drift == false)
    38.         {
    39.             car.rotation = Quaternion.Slerp(car.rotation, wheels.rotation, Time.deltaTime * RotationSpeedCar);
    40.         } else if (car.velocity != Vector3.zero && Y < 0 && Drift == false)
    41.         {
    42.             car.rotation = Quaternion.Slerp(car.rotation, Quaternion.Inverse(wheels.localRotation), Time.deltaTime * RotationSpeedCar);
    43.         }
    44.  
    45.         if (Input.GetButton("Jump"))
    46.         {
    47.             Drift = true;
    48.             car.AddForceAtPosition(turn.forward * DriftForce, turn.position);
    49.         } else
    50.         {
    51.             Drift = false;
    52.         }
    53.        
    54.     }
    55. }
    56.  

    So i have 2 questions:

    1- How do i implement drift? i know a bit about the Math behind a drift, but have a hard time putting it in code. I'm trying to do as close as possible of mario kart, as it's a learning project.

    I tried playing with some "Turn" point behind the car and appllying a force into it, so the car would spin...and tried to move the center of gravity...but i'm not sure what's the best solution...any ideias?

    I was thinking of rotating the car sideways when drifting but keep applying the force in the same direction, however it didn't work out and was a poor solution.



    2- the solution i came up for rotation was something i made on my own, and i know there are probably many other that are better without being realistic. However, following the idea that i'm using, can you give me any pointers regarding what i can do better?

    I know i have some things to work on regarding going backwards, but it's not the focus right now :p

    i will also change from car.velocity to simple add.force later...maybe that's what's causing some problems?




    Sorry for any(many) grammar mistakes, English is my second language.
    Thanks for any help/time!
     
  2. MatheusCohen

    MatheusCohen

    Joined:
    Aug 24, 2017
    Posts:
    57
    Someone can help me?