BaseEnemy.cs 1.54 KB
Newer Older
Cody A Burchett's avatar
Cody A Burchett committed
1 2 3 4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Cody A Burchett's avatar
Cody A Burchett committed
5
[RequireComponent(typeof(Hazard))]
Cody A Burchett's avatar
Cody A Burchett committed
6 7 8
public class BaseEnemy : MonoBehaviour {

    public float speed = 2f;
9 10
    public float maxMoveDistance = 5f;
    protected float moveDistanceLeft;
Cody A Burchett's avatar
Cody A Burchett committed
11 12 13 14 15 16 17
    Rigidbody2D body;
    protected SpriteRenderer sr;
    protected float startMoveTime;

    public Transform rightCollisionPos;
    public Transform leftCollisionPos;

Cody A Burchett's avatar
Cody A Burchett committed
18 19
    public bool hasEnteredView = false;

Cody A Burchett's avatar
Cody A Burchett committed
20 21 22 23 24
    void Start()
    {
        body = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
        startMoveTime = Time.time + .4f;
25
        moveDistanceLeft = maxMoveDistance;
Cody A Burchett's avatar
Cody A Burchett committed
26
    }
Cody A Burchett's avatar
Cody A Burchett committed
27 28
    //move enemy in dir direction
    protected void Move(Vector2 dir)
Cody A Burchett's avatar
Cody A Burchett committed
29
    {
30
        moveDistanceLeft -= (dir * speed * Time.deltaTime).magnitude;
Cody A Burchett's avatar
Cody A Burchett committed
31
        body.transform.Translate(dir * speed * Time.deltaTime);
Cody A Burchett's avatar
Cody A Burchett committed
32
    }
Cody A Burchett's avatar
Cody A Burchett committed
33 34
    //check for collision in dir direction and start horizontal check in collisionPos
    protected bool CheckCollision(Vector2 dir, Vector3 collisionPos)
Cody A Burchett's avatar
Cody A Burchett committed
35
    {
Cody A Burchett's avatar
Cody A Burchett committed
36 37
        RaycastHit2D hitSide = Physics2D.Raycast(collisionPos, dir, .1f);
        RaycastHit2D hitDown = Physics2D.Raycast(collisionPos, -transform.up, .1f);
Cody A Burchett's avatar
Cody A Burchett committed
38

Cody A Burchett's avatar
Cody A Burchett committed
39 40 41 42 43 44
        if (hitDown.collider != null && hitSide.collider == null)
        {
            return true;
        }
        return false;
    }
45 46 47 48 49 50 51 52 53 54
	protected bool CheckCollisionCharge(Vector2 dir, Vector3 collisionPos)
	{
		RaycastHit2D hitSide = Physics2D.Raycast(collisionPos, dir, .1f);

		if (hitSide.collider == null)
		{
			return true;
		}
		return false;
	}
Cody A Burchett's avatar
Cody A Burchett committed
55
}