- Home /
Question by
okjkee · Feb 06, 2020 at 07:36 AM ·
2d gamepositiontransform.positionlookattransform.translate
lookat gameobject 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);
}
}
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;
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612222825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers
I'm trying to displace a UI object in the opposite direction I move a scrollbar 1 Answer
move to a position not being looked at 1 Answer
changing x multiplies x,y and z position for no reason 2 Answers
How Push Players on Network 0 Answers