FlameCrab.cs 1.55 KB
Newer Older
Cody A Burchett's avatar
Cody A Burchett committed
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
using UnityEngine;
using System.Collections;

public class FlameCrab : MonoBehaviour {

    public float moveDelay = 5f;
    public float speed = 5f;
    public float health = 10f;

    float nextMove = 0f;
    Vector3 targetLocation = -Vector3.up;
    Rigidbody2D body;

	// Use this for initialization
	void Start () {
        body = GetComponent<Rigidbody2D>();
	}
	
	// Update is called once per frame
	void Update () {
	    if (nextMove < Time.time)
        {
            if (targetLocation != -Vector3.up)
            {
                if ((transform.position - targetLocation).sqrMagnitude > .15f)
                {
                    Move();
                }
                else
                {
                    nextMove = Time.time + moveDelay;
                    targetLocation = -Vector3.up;
                }
            }
            else
            {
                //find new target location to right or left of enemy
                int spacesToMove = Random.Range(-5, 6);
                RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, spacesToMove, 1);
                if (hit.collider)
                {
                    targetLocation = hit.point;
                }
                else
                {
                    targetLocation = transform.position + (transform.right * spacesToMove);
                }
                
            }
        }
	}

    void Move()
    {
        Vector3 dir = targetLocation - transform.position;
        body.transform.Translate(dir.normalized * speed * Time.deltaTime);
    }
}