-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveBetweenBounds
More file actions
62 lines (50 loc) · 1.36 KB
/
Copy pathMoveBetweenBounds
File metadata and controls
62 lines (50 loc) · 1.36 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveBetweenBounds : MonoBehaviour
{
//boundary variables
public float leftBound;
public float rightBound;
//movement variables
public float direction = 1;
public float speed = 5.0f;
private Vector3 yFlip = new Vector3(0, 180, 0);
//varibales for timer to allow enemy to freeze after hitting player
public float timeToFreeze = 1;
private float timeRemaining;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//keep the enemy moving within its bounds
if (transform.position.x >= rightBound)
{
//flip
transform.Rotate(yFlip);
}
if (transform.position.x <= leftBound)
{
//flip
transform.Rotate(yFlip);
}
//timer for freezing after hitting player
if (timeRemaining <= 0)
{
GetComponent<Animator>().speed = 1;
transform.Translate(Vector3.right * direction * speed * Time.deltaTime);
}
else
{
GetComponent<Animator>().speed = 0;
timeRemaining -= Time.deltaTime;
}
}
public void freeze()
{
timeRemaining = timeToFreeze;
}
}