Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class EnemyPatrol : Enemy
- {
- public LayerMask enemyMask;
- public float speed = 1;
- Rigidbody2D myBody;
- Transform myTrans;
- float myWidth, myHeight;
- protected override void Start() {
- base.Start();
- myTrans = this.transform;
- myBody = this.GetComponent<Rigidbody2D>();
- SpriteRenderer mySprite = this.GetComponent<SpriteRenderer>();
- myWidth = mySprite.bounds.extents.x;
- myHeight = mySprite.bounds.extents.y;
- }
- void FixedUpdate () {
- //Use this position to cast the isGrounded/isBlocked lines from
- Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
- //Check to see if there's ground in front of us before moving forward
- Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
- bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
- //Check to see if there's a wall in front of us before moving forward
- Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * .05f);
- bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos - myTrans.right.toVector2() * .05f, enemyMask);
- //If theres no ground, turn around. Or if I hit a wall, turn around
- if(!isGrounded || isBlocked) {
- Vector3 currRot = myTrans.eulerAngles;
- currRot.y += 180;
- myTrans.eulerAngles = currRot;
- }
- //Always move forward
- Vector2 myVel = myBody.velocity;
- myVel.x = -myTrans.right.x * speed;
- myBody.velocity = myVel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement