-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathcpuBOTS.cs
80 lines (66 loc) · 1.79 KB
/
cpuBOTS.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class cpuBOTS : MonoBehaviour
{
Transform target;
private GameObject finishLine;
private float speed;
private float nextWaypointDistance;
Path path;
int currentWaypoint = 0;
bool reachedEndOfPath = false;
Seeker seeker;
Rigidbody2D rb;
void Start()
{
speed = 8000f;
nextWaypointDistance = 100f;
finishLine = GameObject.FindWithTag("botPlayers");
target = finishLine.transform;
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("updatePath", 0f, .5f);
}
void updatePath()
{
if(seeker.IsDone())
{
seeker.StartPath(rb.position, target.position, OnPathComplete);
}
}
void OnPathComplete(Path p)
{
if(!p.error)
{
path = p;
currentWaypoint = 0;
}
}
// Update is called once per frame
void FixedUpdate()
{
//rb.velocity = new Vector2(40, rb.velocity.y);
rb.constraints = RigidbodyConstraints2D.FreezePositionY;
if(path == null)
return;
if(currentWaypoint >= path.vectorPath.Count)
{
reachedEndOfPath = true;
return;
}
else
{
reachedEndOfPath = false;
}
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 force = direction * speed * Time.deltaTime;
rb.AddForce(force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if(distance < nextWaypointDistance)
{
currentWaypoint++;
}
}
}