Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Enemy_Oppossum : MonoBehaviour {
- public LayerMask enemyMask;
- public float speed = 2;
- Rigidbody2D myBody;
- Transform myTrans;
- float myWidth, myHeight;
- // Use this for initialization
- void Start () {
- myTrans = this.transform;
- myBody = this.GetComponent<Rigidbody2D>();
- SpriteRenderer mySprite = this.GetComponent<SpriteRenderer>();
- myWidth = mySprite.bounds.extents.x;
- myHeight = mySprite.bounds.extents.y;
- }
- // Update is called once per frame
- void Update () {
- // Check to see if theres ground in front of us before moving forward
- Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
- Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
- bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
- Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * 0.05f);
- bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos -myTrans.right.toVector2() * 0.02f, enemyMask);
- //If there's no ground, turn around or if I am blocked, 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