forked from CarrattStudios/ARPG2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlimeController.cs
51 lines (43 loc) · 1.37 KB
/
SlimeController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlimeController : MonoBehaviour {
public float moveSpeed;
private Rigidbody2D myRigidbody;
private bool moving;
private float timeBetweenMove;
private float timeBetweenMoveCounter;
private float timeToMove;
private float timeToMoveCounter;
private Vector3 moveDirection;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
timeBetweenMoveCounter = timeBetweenMove;
timeToMoveCounter = timeToMove;
}
// Update is called once per frame
void Update()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myRigidbody.velocity = moveDirection;
if(timeToMoveCounter < 0f)
{
moving = false;
timeBetweenMoveCounter = timeToMove;
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if(timeBetweenMoveCounter< 0f)
moving = true;
timeToMoveCounter = timeToMove;
moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
}
}
}