Vehicle Physics Car controller for Unity With Free and open-source script

JRS Vehicle Physics Controller – Ultimate Guide for Unity Developers

JRS Vehicle Physics Controller – Ultimate Guide for making a drivable vehicle in Unity

Vehicle Physics Car controller for Unity With Free and open-source script

Welcome to the ultimate guide for the JRS Vehicle Physics Controller! Whether you’re a complete beginner or an experienced Unity developer, this guide will walk you through everything you need to know about using this powerful tool to create realistic and responsive vehicle behavior in your games or simulations.

Note: The JRS Vehicle Physics Controller is free and open-source, and you can find the script on GitHub. It’s perfect for anyone looking to add realistic vehicle physics to their Unity projects.

Realistic Physics

Create realistic vehicle behavior with advanced physics controls.

Mobile Inputs

Supports on-screen mobile inputs for touch-based controls.

Keyboard Inputs

Customizable keyboard inputs for PC-based controls.

Light System

Control vehicle lights, including headlights, brake lights, and more.

Note: AI was used as an assistance of text generating purpose like code, titles, descriptions, info’s, ideas, documenting etc.

Introduction

The JRS Vehicle Physics Controller is a powerful tool that allows you to create realistic and responsive vehicle behavior in your Unity projects. It handles various aspects of vehicle physics, including steering, acceleration, braking, and even dust particle effects. This guide will walk you through the different parameters available in the Inspector of each script and how to use them to customize your vehicle’s behavior.

Tip: If you’re new to Unity, don’t worry! This guide is designed to be beginner-friendly, with step-by-step instructions to help you get started.

Getting Started

To get started with the JRS Vehicle Physics Controller, follow these steps:

  1. Attach the Scripts: Ensure that the required scripts are attached to the GameObject representing your parent vehicle object in the Unity scene. For example, if your vehicle is named AMR_Police_Vehicle, attach the following scripts in the Inspector panel:
    • JS Police Siren (Script)
    • JS Vehicle Light Control (Script)
    • JS Vehicle Controller (Script)
  2. Add Rigidbody and Box Collider: Under the Inspector panel of your parent object, click Add Component and select Rigidbody with the following parameters:
    • Mass: 1500
    • Drag: 0
    • Angular Drag: 0.05
    • Use Gravity: Enable
    Next, create a Box Collider for your vehicle. You can add as many Box Colliders as needed to different parts of the vehicle.

Understanding the Parameters

Each script in the JRS Vehicle Physics Controller comes with various parameters that control your vehicle’s behavior. Let’s break them down:

JS Police Siren (Script)

This script is specifically designed for police vehicles. It allows you to control the siren sound and the flickering of red and blue lights. Here’s how to set it up:

  • Siren Sound: Drag and drop an Audio Source component here to be used as the police siren sound.
  • Red Lights: Assign game objects or lights that represent the red lights of the police siren.
  • Blue Lights: Assign game objects or lights that represent the blue lights of the police siren.

Important: Remember to disable any lights or glowing objects in the Inspector panel so that they only activate when you press the desired key.

JS Vehicle Light Control (Script)

This script allows you to control various lights on your vehicle, including headlights, signal lights, reverse lights, and brake lights. Here are the default keyboard inputs:

  • P: Toggle Police Siren ON/OFF
  • H: Toggle headlights ON/OFF
  • T: Toggle signal lights ON/OFF
  • E: Toggle extra lights ON/OFF
  • S: Activate reverse lights when reversing
  • Spacebar: Activate brake lights when braking

Camera Setup

Setting up the camera is crucial for a good driving experience. The JRS Vehicle Physics Controller supports multiple camera views, including follow and orbit cameras. Here’s how to set them up:

JS Follow Camera (Script)

This script allows the camera to follow your vehicle. Here are the key parameters:

  • Target: Assign an empty GameObject that the camera will follow.
  • Offset: Adjust the X, Y, and Z values to position the camera relative to the target.
  • Horizontal Spring Constant: Controls how much the camera springs back to its desired position horizontally.
  • Horizontal Damping Constant: Controls how smooth or jerky the camera’s movements are.

✅ If you need only the Follow Camera Script you can get it on this link.

JS Orbit Camera (Script)

This script allows the camera to orbit around your vehicle. Key parameters include:

  • Target: Assign an empty GameObject that the camera will orbit around.
  • Rotation Speed: Controls how fast the camera rotates around the target.
  • Zoom Speed: Controls how fast the camera zooms in and out.

Driving the Vehicle

Once you’ve set up your vehicle and camera, it’s time to drive! The JRS Vehicle Physics Controller supports both keyboard and mobile inputs. Here’s how to configure them:

Keyboard Inputs

By default, you can use the following keys to control your vehicle:

  • W: Accelerate
  • A: Steer left
  • S: Reverse
  • D: Steer right
  • Spacebar: Brake

On-Screen Buttons (Mobile Input)

For mobile devices, you can create on-screen buttons to control the vehicle. Here’s how:

  1. Create a Canvas by right-clicking in the Hierarchy panel and selecting UI > Canvas.
  2. Attach the JSInputController (Script) and JSCameraSwitcher (Script) to the Canvas.
  3. Create image buttons for each input (e.g., accelerate, brake, steer left, steer right) and assign them in the Inspector.

Troubleshooting

If you encounter any issues while using the JRS Vehicle Physics Controller, here are some common solutions:

Hierarchy Structure Error

If you see a NullReferenceException, check your Hierarchy structure. Ensure that the following objects are present:

  • Camera: Your scene should have at least two cameras: Camera Follow and Camera Orbit.
  • Canvas: Even if you’re making a game for desktop, the Canvas with all the buttons should be present in the Hierarchy.
  • EventSystem: This is required for on-screen mobile input.

Light System Not Functioning

If the brake and reverse lights don’t work on mobile, you may need to uncomment certain lines in the JSVehicleLightControl script.

    // Uncomment the below function so that the reverse light for mobile will work
          
     if (mobileInputController.revButton.IsButtonPressed())
     {
         ToggleReverseLights(true);
     }
          
     if (mobileInputController.revButton.IsButtonReleased())
     {
         ToggleReverseLights(false);
     }
            
    //Uncomment the below function so that the the brake light for mobile will work
        
     if (mobileInputController.brakeButton.IsButtonPressed())
     {
         ToggleBrakeLights(true);
     }
        
     if (mobileInputController.brakeButton.IsButtonReleased())
     {
         ToggleBrakeLights(false);
     }
                
            

Features

The JRS Vehicle Physics Controller comes packed with features that make it a must-have for any Unity developer working on vehicle-based games. Here are some of the key features:

  • Realistic Vehicle Physics: Control steering, acceleration, braking, and more with realistic physics.
  • Customizable Inputs: Supports both keyboard and mobile inputs, with the ability to customize keys and buttons.
  • Light System: Control headlights, brake lights, reverse lights, and more.
  • Camera Views: Switch between follow and orbit cameras for different perspectives.

Pro Tip: Check out the Asset Store for more 3D vehicles and assets to use with the JRS Vehicle Physics Controller.

📥 Download the complete Unity project from this link.

3 responses to “Vehicle Physics Car controller for Unity With Free and open-source script”

  1. how do i lower the suspension i tried everything

  2. Samborlang says:

    If you are Unity version 6000.0.23f1 LTS, there is a bug affecting the physics in this Unity Version, which is why adjustments to values during play mode only offer a temporary fix.

    To resolve this issue, I recommend updating to the latest version of Unity 6 (6000.0.27f1 or later), where this problem has been addressed. The latest version of Unity 6 work seamlessly without any complications. Thanks

  3. Samborlang says:

    If you experience any issues with the input system in the newer version Unity 6, please follow this steps provided in this link [https://jermesa.com/fixes-invalidoperationexception-you-are-trying-to-read-input-using-the-unityengine-input-class-but-you-have-switched-active-input-handling-to-input-system-package-in-player-settings/].

Leave a Reply

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