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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarDrive : MonoBehaviour {
private Rigidbody car;
public Transform wheels;
public Transform turn;
public Transform Center;
public float Speed;
public float RotationSpeedWheels;
public float RotationSpeedCar;
public float DriftForce;
public float Y;
public float X;
public float Rotation;
public bool Drift;
void Start () {
car = GetComponent<Rigidbody>();
}
void Update () {
X = Input.GetAxis("Horizontal");
Y = Input.GetAxis("Vertical");
car.velocity = transform.right * Y * Speed;
wheels.rotation = Quaternion.Slerp(wheels.rotation, Quaternion.Euler(0, car.rotation.eulerAngles.y + X * Rotation, 0), Time.deltaTime * RotationSpeedWheels);
if (car.velocity != Vector3.zero && Y>0 && Drift == false)
{
car.rotation = Quaternion.Slerp(car.rotation, wheels.rotation, Time.deltaTime * RotationSpeedCar);
} else if (car.velocity != Vector3.zero && Y < 0 && Drift == false)
{
car.rotation = Quaternion.Slerp(car.rotation, Quaternion.Inverse(wheels.localRotation), Time.deltaTime * RotationSpeedCar);
}
if (Input.GetButton("Jump"))
{
Drift = true;
car.AddForceAtPosition(turn.forward * DriftForce, turn.position);
} else
{
Drift = false;
}
}
}
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
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!