-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackPlayer
More file actions
64 lines (52 loc) · 1.49 KB
/
Copy pathTrackPlayer
File metadata and controls
64 lines (52 loc) · 1.49 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrackPlayer : MonoBehaviour
{
//tracking variables
public int trackMin;
public int trackMax;
private float playerX;
//movement variables
public float direction = 1;
public float speed = 5.0f;
private Vector3 yFlip = new Vector3(0, 180, 0);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
playerX = GameObject.FindWithTag("Player").transform.position.x;
if (playerX > trackMin && playerX < trackMax)
{
GetComponent<Animator>().speed = 1;
//player is within range, move towards them
if (playerX <= transform.position.x)
{
//player is to the left of the enemy
if(direction == 1)
{
transform.Rotate(yFlip);
}
direction = -1;
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
else
{
//player is to the right
if (direction == -1)
{
transform.Rotate(yFlip);
}
direction = 1;
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
else
{
GetComponent<Animator>().speed = 0;
}
}
}