Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Enemy : MonoBehaviour
- {
- [SerializeField] private float moveSpeed = 5f;
- [SerializeField] private LayerMask platformLayerMask;
- private Collider2D currentCollider;
- private Vector3 currentSize;
- private void Start()
- {
- currentCollider = GetComponent<BoxCollider2D>();
- currentSize = currentCollider.bounds.size;
- }
- private void Update()
- {
- Move();
- IsMovingLeft();
- }
- private void Move()
- {
- if (IsMovingLeft())
- {
- transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
- }
- else if (!IsMovingLeft())
- {
- transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
- }
- }
- private bool IsMovingLeft()
- {
- if (IsMovingLeft())
- {
- RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, Vector2.left, currentSize.x / 2, platformLayerMask);
- if (raycastHit2D.collider != null)
- {
- return false;
- }
- return true;
- }
- else
- {
- RaycastHit2D raycastHit2D = Physics2D.Raycast(transform.position, Vector2.right, currentSize.x / 2, platformLayerMask);
- if (raycastHit2D.collider != null)
- {
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement