Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Enemy : MonoBehaviour
  4. {
  5. [SerializeField] private float moveSpeed = 5f;
  6. [SerializeField] private LayerMask platformLayerMask;
  7.  
  8. private Collider2D currentCollider;
  9. private Vector3 currentSize;
  10.  
  11. private void Start()
  12. {
  13. currentCollider = GetComponent<BoxCollider2D>();
  14. currentSize = currentCollider.bounds.size;
  15. }
  16.  
  17. private void Update()
  18. {
  19. Move();
  20. IsMovingLeft();
  21. }
  22.  
  23. private void Move()
  24. {
  25. if (IsMovingLeft())
  26. {
  27. transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
  28. }
  29. else if (!IsMovingLeft())
  30. {
  31. transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
  32. }
  33. }
  34.  
  35. private bool IsMovingLeft()
  36. {
  37. if (IsMovingLeft())
  38. {
  39. RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, Vector2.left, currentSize.x / 2, platformLayerMask);
  40.  
  41. if (raycastHit2D.collider != null)
  42. {
  43. return false;
  44. }
  45.  
  46. return true;
  47. }
  48. else
  49. {
  50. RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, Vector2.right, currentSize.x / 2, platformLayerMask);
  51. if (raycastHit2D.collider != null)
  52. {
  53. return true;
  54. }
  55. return false;
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement