new car control
This commit is contained in:
45
Assets/Scripts/CarControl.cs
Normal file
45
Assets/Scripts/CarControl.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CarControl : MonoBehaviour
|
||||
{
|
||||
|
||||
public float motorTorque = 2000;
|
||||
public float brakeTorque = 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 Update()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CarControl.cs.meta
Normal file
11
Assets/Scripts/CarControl.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a23c7d66d3ff94847a946cf7b30ca1b7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,7 +9,6 @@ public class carcontrolv2 : MonoBehaviour
|
||||
public float maxSpeed = 20;
|
||||
public float steeringRange = 30;
|
||||
public float steeringRangeAtMaxSpeed = 10;
|
||||
public float centreOfGravityOffset = -1f;
|
||||
|
||||
WheelControl[] wheels;
|
||||
Rigidbody rigidBody;
|
||||
@@ -19,9 +18,6 @@ public class carcontrolv2 : MonoBehaviour
|
||||
{
|
||||
rigidBody = GetComponent<Rigidbody>();
|
||||
|
||||
// Adjust center of mass vertically, to help prevent the car from rolling
|
||||
rigidBody.centerOfMass += Vector3.up * centreOfGravityOffset;
|
||||
|
||||
// Find all child GameObjects that have the WheelControl script attached
|
||||
wheels = GetComponentsInChildren<WheelControl>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user