Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody2D), typeof(Animator))]
- public class Patrol : MonoBehaviour
- {
- public float speed;
- private float waitTime;
- public float startWaitTime;
- public Transform[] moveSpots;
- private int currentSpot = 0;
- private SpriteRenderer _sr;
- private Animator _animator;
- private Rigidbody2D _rb;
- private static readonly int Move = Animator.StringToHash("move");
- private static readonly int Disable1 = Animator.StringToHash("disable");
- private AudioSource _as;
- void Start()
- {
- waitTime = startWaitTime;
- _sr = GetComponent<SpriteRenderer>();
- _rb = GetComponent<Rigidbody2D>();
- _animator = GetComponent<Animator>();
- _as = GetComponent<AudioSource>();
- _as.Play();
- _animator.SetBool(Move, true);
- }
- void Update()
- {
- var newPosition =
- Vector2.MoveTowards(transform.position, moveSpots[currentSpot].position, speed * Time.deltaTime);
- var shift = newPosition - (Vector2)transform.position;
- var scale = transform.localScale;
- scale.x = Mathf.Sign(shift.x);
- transform.localScale = scale;
- _rb.MovePosition(newPosition);
- if (Vector2.Distance(transform.position, moveSpots[currentSpot].position) < 0.01f)
- {
- if (waitTime <= 0)
- {
- currentSpot = (currentSpot + 1) % moveSpots.Length;
- waitTime = startWaitTime;
- if (!_as.isPlaying)
- _as.Play();
- _animator.SetBool(Move, true);
- }
- else
- {
- waitTime -= Time.deltaTime;
- if (_as.isPlaying)
- _as.Stop();
- _animator.SetBool(Move, false);
- }
- return;
- }
- }
- public void Disable()
- {
- _animator.SetTrigger(Disable1);
- Debug.Log($"Enemy {gameObject.name} disabled");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement