-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpponentCar.cs
50 lines (42 loc) · 1.38 KB
/
OpponentCar.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpponentCar : MonoBehaviour
{
[Header("Car Engine")]
public float movingSpeed;
public float turningSpeed = 50f;
public float breakSpeed = 12f;
[Header("Destination Var")]
public Vector3 destination;
public bool destinationReached;
private void Update()
{
Drive();
}
public void Drive()
{
if (transform.position != destination)
{
Vector3 destinationDirection = destination - transform.position;
destinationDirection.y = 0;
float destinationDistance = destinationDirection.magnitude;
if (destinationDistance >= breakSpeed)
{
destinationReached = false;
Quaternion targetRotation = Quaternion.LookRotation(destinationDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, turningSpeed * Time.deltaTime);
transform.Translate(Vector3.forward * movingSpeed * Time.deltaTime);
}
else
{
destinationReached = true;
}
}
}
public void LocateDestination(Vector3 destination)
{
this.destination = destination;
destinationReached = false;
}
}