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 EnemyPatrol : MonoBehaviour
- {
- public float moveSpeed;
- public bool movingRight;
- public Transform wallCheck;
- public LayerMask whatIsWall;
- public float wallCheckRadius;
- private bool notAtEdge;
- public Transform edgeCheck;
- private bool hittingWall;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- hittingWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
- notAtEdge = Physics2D.OverlapCircle(edgeCheck.position, wallCheckRadius, whatIsWall);
- if (hittingWall || !notAtEdge)
- {
- movingRight = !movingRight;
- }
- if (movingRight)
- {
- transform.localScale = new Vector3(-1f, 1f, 1f);
- GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
- }
- else
- {
- transform.localScale = new Vector3(1f, 1f, 1f);
- GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement