Repository navigation
Expand file tree
/
Copy pathCarControl.cs
More file actions
70 lines (59 loc) · 2.22 KB
/
Copy pathCarControl.cs
File metadata and controls
70 lines (59 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using UnityEngine;
public class CarControl : MonoBehaviour
{
[Header("Car Properties")]
public float speed = 20.0f;
public float turnSpeed = 45.0f;
public float acceleration = 5.0f;
public float deceleration = 8.0f;
public float steeringRangeAtMaxSpeed = 30.0f;
// For wheel visual rotation
public GameObject[] wheelMeshes;
public float wheelRotationSpeed = 360.0f;
private float currentSpeed = 0.0f;
private float currentRotation = 0.0f;
void Update()
{
// Get input from traditional Input system
float verticalInput = Input.GetAxis("Vertical");
float horizontalInput = Input.GetAxis("Horizontal");
// Handle acceleration and deceleration
if (verticalInput != 0)
{
// Accelerate
currentSpeed += verticalInput * acceleration * Time.deltaTime;
}
else
{
// Decelerate when no input
currentSpeed = Mathf.MoveTowards(currentSpeed, 0, deceleration * Time.deltaTime);
}
// Clamp speed
currentSpeed = Mathf.Clamp(currentSpeed, -speed/2, speed);
// Calculate turn amount based on speed
float speedFactor = Mathf.Abs(currentSpeed) / speed;
float turnAmount = horizontalInput * Mathf.Lerp(turnSpeed, steeringRangeAtMaxSpeed, speedFactor);
// Only turn if we're moving
if (Mathf.Abs(currentSpeed) > 0.1f)
{
// Apply rotation
transform.Rotate(Vector3.up, turnAmount * Time.deltaTime);
}
// Move the car forward/backward
transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
// Rotate wheel meshes for visual effect
if (wheelMeshes != null && wheelMeshes.Length > 0)
{
float wheelRotation = currentSpeed * wheelRotationSpeed * Time.deltaTime;
foreach (GameObject wheel in wheelMeshes)
{
wheel.transform.Rotate(wheelRotation, 0, 0);
}
}
}
// Optional: Visual display of current speed
void OnGUI()
{
GUI.Label(new Rect(10, 10, 200, 20), "Speed: " + currentSpeed.ToString("F1"));
}
}