Unity Beginner Tutorial: Car Controller Script for Your Game

Codynn
3 Min Read

Are you diving into Unity and looking to create a car controller for your game? Here’s a step-by-step guide on implementing a basic car controller script that you can use and modify for your Unity projects.

Introduction

In this tutorial, we’ll walk you through a simple car controller script written in C# for Unity. This script will handle car movement, steering, and braking. Perfect for beginners, this code snippet will help you understand how to manipulate wheel colliders and control car physics in Unity.

CarController Script

Here’s the complete CarController script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarController : MonoBehaviour
{
    private float horizontalInput, verticalInput;
    private float currentSteerAngle, currentbreakForce;
    private bool isBreaking;

    // Settings
    [SerializeField] private float motorForce, breakForce, maxSteerAngle;

    // Wheel Colliders
    [SerializeField] private WheelCollider frontLeftWheelCollider, frontRightWheelCollider;
    [SerializeField] private WheelCollider rearLeftWheelCollider, rearRightWheelCollider;

    // Wheels
    [SerializeField] private Transform frontLeftWheelTransform, frontRightWheelTransform;
    [SerializeField] private Transform rearLeftWheelTransform, rearRightWheelTransform;

    private void FixedUpdate() {
        GetInput();
        HandleMotor();
        HandleSteering();
        UpdateWheels();
    }

    private void GetInput() {
        // Steering Input
        horizontalInput = Input.GetAxis("Horizontal");

        // Acceleration Input
        verticalInput = Input.GetAxis("Vertical");

        // Breaking Input
        isBreaking = Input.GetKey(KeyCode.Space);
    }

    private void HandleMotor() {
        frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
        frontRightWheelCollider.motorTorque = verticalInput * motorForce;
        currentbreakForce = isBreaking ? breakForce : 0f;
        ApplyBreaking();
    }

    private void ApplyBreaking() {
        frontRightWheelCollider.brakeTorque = currentbreakForce;
        frontLeftWheelCollider.brakeTorque = currentbreakForce;
        rearLeftWheelCollider.brakeTorque = currentbreakForce;
        rearRightWheelCollider.brakeTorque = currentbreakForce;
    }

    private void HandleSteering() {
        currentSteerAngle = maxSteerAngle * horizontalInput;
        frontLeftWheelCollider.steerAngle = currentSteerAngle;
        frontRightWheelCollider.steerAngle = currentSteerAngle;
    }

    private void UpdateWheels() {
        UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
        UpdateSingleWheel(frontRightWheelCollider, frontRightWheelTransform);
        UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
        UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
    }

    private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform) {
        Vector3 pos;
        Quaternion rot; 
        wheelCollider.GetWorldPose(out pos, out rot);
        wheelTransform.rotation = rot;
        wheelTransform.position = pos;
    }
}

Explanation

  1. Input Handling:
  • GetInput(): Captures user inputs for steering, acceleration, and braking.
  1. Motor Control:
  • HandleMotor(): Applies torque to the wheel colliders based on user input and manages braking force.
  1. Steering Control:
  • HandleSteering(): Adjusts the steer angle of the front wheels.
  1. Wheel Updates:
  • UpdateWheels(): Updates the position and rotation of each wheel to match the wheel colliders.
  1. Wheel Pose Update:
  • UpdateSingleWheel(): Synchronizes the visual representation of the wheels with the physical wheel colliders.

Getting Started

  1. Attach this script to your car GameObject in Unity.
  2. Assign the appropriate Wheel Colliders and Wheel Transforms in the Unity Editor.
  3. Customize the motorForce, breakForce, and maxSteerAngle to fit your needs.

Conclusion

This car controller script is a great starting point for beginners looking to understand vehicle mechanics in Unity. Experiment with the settings and modify the script as needed to better suit your game’s requirements. Happy coding!

For more detailed explanations and a visual walkthrough, check out our YouTube tutorial. Don’t forget to like, subscribe, and hit the notification bell to stay updated with our latest content!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *