- Home /
how to force enemy looat he moved?
before start, create empty project, add sprite to scene, and add to sprite this script, and here is the problem, how to forse enemy, look at direction he moved;
using UnityEngine;
public class Randomposition: MonoBehaviour
{
public float minX = -20f;
public float maxX = 20f;
public float minY = -20f;
public float maxY = 20f;
public float speed = 1f;
Vector2 targetPosition;
private void Start()
{
targetPosition = GetRandomPosition();
}
private void Update()
{
if ((Vector2)transform.position != targetPosition)
{
transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
else
{
targetPosition = GetRandomPosition();
}
}
Vector2 GetRandomPosition()
{
float randomX = Random.Range(minX, maxX);
float randomY = Random.Range(minY, maxY);
return new Vector2(randomX, randomY);
}
}
Comment