added AI
This commit is contained in:
1 parent
8ea88f9aaf
commit
3c6679ab04
6 files changed
+821
-3
No files matched your search
@@ -0,0 +1,41 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Gameplay
|
||||
{
|
||||
public class AICarController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float speed = 15f;
|
||||
[SerializeField] private float stoppingDistance = 2f;
|
||||
[SerializeField] private float turnSpeed = 3f;
|
||||
|
||||
private Vector3[] _waypoints;
|
||||
private int _currentWaypointIndex;
|
||||
|
||||
public void SetRoute(Vector3[] waypoints)
|
||||
{
|
||||
_waypoints = waypoints;
|
||||
_currentWaypointIndex = 0;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_waypoints == null || _waypoints.Length == 0) return;
|
||||
|
||||
var target = _waypoints[_currentWaypointIndex];
|
||||
var direction = target - transform.position;
|
||||
|
||||
if (direction.magnitude > 0.1f)
|
||||
{
|
||||
var targetRotation = Quaternion.LookRotation(direction, Vector3.up);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
transform.Translate(Vector3.forward * (speed * Time.deltaTime), Space.Self);
|
||||
|
||||
if (direction.magnitude < stoppingDistance)
|
||||
{
|
||||
_currentWaypointIndex = (_currentWaypointIndex + 1) % _waypoints.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9530712812274eb98a8a24d8feda7c9c
|
||||
timeCreated: 1769531987
|
||||
@@ -1,3 +1,4 @@
|
||||
using Code.Gameplay;
|
||||
using Code.Track;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -8,6 +9,7 @@ namespace Code.UI
|
||||
[SerializeField] private TrackBuilder trackBuilder;
|
||||
[SerializeField] private Transform playerSpawn;
|
||||
[SerializeField] private Transform finishTrigger;
|
||||
[SerializeField] private AICarController[] aiCars;
|
||||
|
||||
private TrackData _currentTrack;
|
||||
|
||||
@@ -23,6 +25,12 @@ namespace Code.UI
|
||||
|
||||
finishTrigger.position = _currentTrack.FinishPosition;
|
||||
finishTrigger.rotation = _currentTrack.FinishRotation;
|
||||
|
||||
foreach (var aiCar in aiCars)
|
||||
{
|
||||
aiCar.transform.position = _currentTrack.Waypoints[0] + Random.insideUnitSphere * 2f;
|
||||
aiCar.GetComponent<AICarController>().SetRoute(_currentTrack.Waypoints.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user