Scriptum/Assets/Scripts/Enemies' Scprits/PatrollingEnemy.cs

53 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PatrollingEnemy : Enemy
{
public Transform[] path;
public int currentPoint;
public Transform currentGoal;
public float roundingDistance;
private Rigidbody2D myRigidbody;
//public Transform target;
public Animator anim;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(transform.position, path[currentPoint].position) > roundingDistance)
{
Vector3 temp = Vector3.MoveTowards(transform.position, path[currentPoint].position, moveSpeed * Time.deltaTime);
//changeAnim(temp - transform.position);
myRigidbody.MovePosition(temp);
}
else
{
changeGoal();
}
}
private void changeGoal()
{
if (currentPoint == path.Length - 1)
{
currentPoint = 0;
currentGoal = path[0];
}
else
{
currentPoint++;
currentGoal = path[currentPoint];
}
}
}