Car steering on a Rigidbody (Mario Kart)
Hey there, I am fiddling around with a little Mario Kart like game in Unity. Currently, I am working on the car controller. I don't use Wheel colliders whatsoever, just a simple car model with a Rigidbody and Box Collider attached.
I managed ac/deceleration including the car to stop after a certain time (if no input by the user). However, I still can't figure out how to handle the steering.
What I want to achieve:
Only steerable when velocity != 0, so only while moving forward/backward
No or very little drag when turning
Steering when driving backwards
Basically, this is what I am looking for: https://www.youtube.com/watch?v=ASWgJvuQhTA
My code until now:
using UnityEngine;
using System.Collections;
public class kartcontrol : MonoBehaviour {
private Rigidbody rb;
private float inputX = 0.0f;
private float inputY = 0.0f;
public float currentSpeed = 0.0f;
public float maxSpeed = 20f;
public float acceleration = 10f;
public float maxReverseSpeed = 10f;
public float deceleration = 5;
public float smoothStop = 5f;
public float steeringAngle = 25f;
private float velo;
Quaternion targetRotation;
void Start()
{
rb = GetComponent<Rigidbody>();
targetRotation = Quaternion.identity;
}
void Update()
{
inputX = Input.GetAxis("Horizontal");
inputY = Input.GetAxis("Vertical");
}
void FixedUpdate()
{
if (inputY > 0)
{
// forward
if (currentSpeed < maxSpeed)
{
currentSpeed += acceleration;
}
}
else if (inputY < 0)
{
// backward
if (currentSpeed > -maxReverseSpeed)
{
currentSpeed -= deceleration;
}
}
else
{
// Stopping the car when no user input given
var velocity = rb.velocity;
var localVel = transform.InverseTransformDirection(velocity);
currentSpeed = Mathf.SmoothDamp(currentSpeed, 0f, ref velo, Time.deltaTime * smoothStop );
}
// Apply currentspeed as rigidbody velocity
rb.velocity += transform.forward * currentSpeed;
Debug.Log(currentSpeed + " --- " +rb.velocity);
}
}
Thanks in advance :)
Your answer
Follow this Question
Related Questions
Mario kart controller physics 0 Answers
Rotating around a point using Rigidbody? 0 Answers