Hello everyone, the following is the code below for my top down shooter for the enemy. However I found a weird rotation issue where if I use the player to collide with the enemy, then the enemy has its rotation vector messed up. In a way if I spin the enemy somehow through colliding with it, then the enemy will face the player sideways and will keep trying to look at the player sideways instead of face forward. When the enemy gets close and is able to collide with the player on its own the vector seems to back to normal and the enemy will face forward again.
I feel like the main issue is in Update() part of the code where the Slerp and various other calculations are happening.
using UnityEngine;
using System.Collections;
public class EnemyScript: MonoBehaviour {
public float EnemyHealth = 100;
public float speed;
public float Rspeed;
public Transform player;
public Transform target;
void Update() {
Vector3 vectorToTarget = target.transform.position - transform.position;
float z = Mathf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * Mathf.Rad2Deg - 90;
Quaternion q = Quaternion.AngleAxis(z, transform.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * Rspeed);
}
void FixedUpdate () {
GetComponent<Rigidbody2D>().AddForce (gameObject.transform.up * speed);
}
}