FlameCrab.cs 1.72 KB
Newer Older
Cody A Burchett's avatar
Cody A Burchett committed
1 2 3 4 5
using UnityEngine;
using System.Collections;

public class FlameCrab : MonoBehaviour {

Cody A Burchett's avatar
Cody A Burchett committed
6 7 8 9 10
    //update once grid is ready

    //array of positions to move to once grid is ready
    //Vector3 [] positions;

Cody A Burchett's avatar
Cody A Burchett committed
11 12 13
    public float moveDelay = 5f;
    public float speed = 5f;
    public float health = 10f;
Cody A Burchett's avatar
Cody A Burchett committed
14
    public int moveDistance = 5;
Cody A Burchett's avatar
Cody A Burchett committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

    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)
            {
Cody A Burchett's avatar
Cody A Burchett committed
31
                if ((transform.position - targetLocation).sqrMagnitude > .5f)
Cody A Burchett's avatar
Cody A Burchett committed
32 33 34 35 36 37 38 39 40 41 42 43
                {
                    Move();
                }
                else
                {
                    nextMove = Time.time + moveDelay;
                    targetLocation = -Vector3.up;
                }
            }
            else
            {
                //find new target location to right or left of enemy
Cody A Burchett's avatar
Cody A Burchett committed
44
                int spacesToMove = Random.Range(-moveDistance, moveDistance+1);
Cody A Burchett's avatar
Cody A Burchett committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                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);
    }
}