im trying to get my floating enemy to always face the player, i’ve tried using transform.lookat and changed to x and y to 0 but it still rotates in the third dimension which looks horrible, the ridgidbody is set to not rotate, is there a better way to do this, its a 2d sidescroller so im just wanting it to stay flat and rotate towards the player on the z, im probably being an idiot and missing something simple
public class FloatingEyeController : MonoBehaviour
{
Rigidbody2D rb;
Animator anim;
public float moveSpeed;
public Transform player;
public float searchRadius;
public float attackRadius;
public bool inSearchRange;
public bool inAttackRange;
Vector3 lookat;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
player = GameObject.FindGameObjectWithTag("Player").transform;
anim = GetComponent<Animator>();
}
private void Update()
{
lookat = new Vector3(0,0, player.position.z);
if (Vector2.Distance(player.position, gameObject.transform.position) < searchRadius)
{
inSearchRange = true;
Debug.Log("In Search Range");
}
if (Vector2.Distance(player.position, gameObject.transform.position) > searchRadius)
{
inSearchRange = false;
}
if (Vector2.Distance(player.position, gameObject.transform.position) < attackRadius)
{
inAttackRange = true;
Debug.Log("In Attack Range");
}
if (Vector2.Distance(player.position, gameObject.transform.position) > attackRadius)
{
inAttackRange = false;
}
if (inAttackRange)
{
transform.LookAt(lookat);
}
}
private void FixedUpdate()
{
if (inSearchRange)
{
rb.AddForce((player.position - transform.position) * moveSpeed);
if (inAttackRange)
{
rb.velocity = Vector2.zero;
anim.SetTrigger("Attacking");
}
}
}
}