Utilizing Multiple Scripts for Car Movement in a Racing Game

Hello~!

I’m a beginner-level game developer, and I’m trying to create custom driving physics for my racing game.

I’d like to know how I could utilize the primary Script of my car - known as “Car_Move.cs” - to directly access and control two other scripts attached to my car (known as “Simple_Car_Rotation.cs” and “Simple_Car_Steering.cs”) using the Arrow Keys. I’m trying to use Car_Move to do two things:

  1. Synchronize the movement of the car’s rear wheels (attached to Simple_Car_Rotation) with the movement of the car body so that both the car body and the all of the wheels move forward.

  2. Synchronize the rotation of the car’s front wheels (attached to Simple_Car_Steering) so that the car turns in the same direction the front wheels are facing.

My current setup for the Car_Move Script is this:

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

public class Car_Move : MonoBehaviour
{
public GameObject Car_Root;

private Simple_Steering_Script SimpleSteering;
private Simple_Rotation_Script SimpleRotation;

private void Awake()
{
    SimpleSteering = GetComponent<Simple_Steering_Script>();
    SimpleRotation = GetComponent<Simple_Rotation_Script>();
}

void Start()
{
    
}

void Update()
{
    if (Input.GetKey(KeyCode.UpArrow))
        transform.Translate(new Vector3(0f, 0f, 35f));

    if (Input.GetKey(KeyCode.DownArrow))
        transform.Translate(new Vector3(0f, 0f, -15f));
}

}

If anybody could help me figure this out, I’d be immensely grateful. Thank you in advance to anyone and everyone who helps out, and I look forward to hearing from you~!

Sincerely,

  • Samuel Pierce

To do that, you have to make some public functions in Simple_Steering_Script and Simple_Rotation_Script to be able to call them from Car_Move. Here is an example with a simplified code:

public class Simple_Steering_Script : MonoBehaviour
{
	public void UpdateSteering()
	{
		// Do whatever
	}
}

Then, you can call that function from Car_Move to sort of synchronize the steering update:

public class Car_Move : MonoBehaviour 
{ 
	private Simple_Steering_Script SimpleSteering;

	private void Awake()
	{
		SimpleSteering = GetComponent<Simple_Steering_Script>();
	}

	void Update()
	{
		if (Input.GetKey(KeyCode.LeftArrow))
		{
			SimpleSteering.UpdateSteering();
		}
	}
}

I hope this helps!