Files
racesm/Assets/Scripts/CarControl.cs
2024-04-05 11:23:18 +02:00

45 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarControl : MonoBehaviour
{
public float motorTorque = 2000;
public float maxSpeed = 20;
public float steeringRange = 30;
//public float steeringRangeAtMaxSpeed = 10;
WheelControl[] wheels;
Rigidbody rigidBody;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
// Find all child GameObjects that have the WheelControl script attached
wheels = GetComponentsInChildren<WheelControl>();
}
// Update is called once per frame
void FixedUpdate()
{
float vInput = Input.GetAxis("Vertical");
float hInput = Input.GetAxis("Horizontal");
foreach (var wheel in wheels)
{
// Apply steering to Wheel colliders that have "Steerable" enabled
if (wheel.steerable)
{
wheel.WheelCollider.steerAngle = hInput * steeringRange;
}
// Apply torque to Wheel colliders that have "Motorized" enabled
if (wheel.motorized)
{
wheel.WheelCollider.motorTorque = vInput * motorTorque;
}
}
}
}