Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Enemy_Oppossum : MonoBehaviour {
  6.  
  7. public LayerMask enemyMask;
  8. public float speed = 2;
  9. Rigidbody2D myBody;
  10. Transform myTrans;
  11. float myWidth, myHeight;
  12.  
  13. // Use this for initialization
  14. void Start () {
  15. myTrans = this.transform;
  16. myBody = this.GetComponent<Rigidbody2D>();
  17. SpriteRenderer mySprite = this.GetComponent<SpriteRenderer>();
  18. myWidth = mySprite.bounds.extents.x;
  19. myHeight = mySprite.bounds.extents.y;
  20. }
  21.  
  22. // Update is called once per frame
  23. void Update () {
  24.  
  25. // Check to see if theres ground in front of us before moving forward
  26. Vector2 lineCastPos = myTrans.position.toVector2() - myTrans.right.toVector2() * myWidth + Vector2.up * myHeight;
  27. Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
  28. bool isGrounded = Physics2D.Linecast(lineCastPos, lineCastPos + Vector2.down, enemyMask);
  29. Debug.DrawLine(lineCastPos, lineCastPos - myTrans.right.toVector2() * 0.05f);
  30. bool isBlocked = Physics2D.Linecast(lineCastPos, lineCastPos -myTrans.right.toVector2() * 0.02f, enemyMask);
  31.  
  32. //If there's no ground, turn around or if I am blocked, turn around
  33. if (!isGrounded || isBlocked)
  34. {
  35. Vector3 currRot = myTrans.eulerAngles;
  36. currRot.y += 180;
  37. myTrans.eulerAngles = currRot;
  38. }
  39.  
  40. // Always move forward
  41. Vector2 myVel = myBody.velocity;
  42. myVel.x = -myTrans.right.x * speed;
  43. myBody.velocity = myVel;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement