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

52 lines
1.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-05-18 21:56:39 +02:00
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];
}
}
}