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 GameObject pointA;
- public GameObject pointB;
- private Rigidbody rb;
- private Transform currentPoint;
- public float speed;
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- currentPoint = pointB.transform;
- }
- // Update is called once per frame
- void Update()
- {
- Vector2 point = currentPoint.position - transform.position;
- if (currentPoint == pointB.transform)
- {
- rb.velocity = new Vector2(speed, 0);
- }
- else
- {
- rb.velocity = new Vector2(-speed, 0);
- }
- if (Vector2.Distance(transform.position, currentPoint.position) < 2f && currentPoint == pointB.transform)
- {
- currentPoint = pointA.transform;
- }
- if (Vector2.Distance(transform.position, currentPoint.position) < 2f && currentPoint == pointA.transform)
- {
- currentPoint = pointB.transform;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement