2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision.
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);
}
}
Answer by Rasgard · Jun 27, 2016 at 05:40 AM
I think I figured it out, my Rigidbody2D had 0 Angular Drag. Combine that with no gravity, I had physics forces constantly affecting the object when I hit it, thus causing it to go crazy and face me with its wrong sides. Thank you ChrisAngelMindmapfreak.
Answer by Mindmapfreak · Jun 26, 2016 at 02:17 PM
Why is your enemy moving along the its up-axis and rotating around its forward-axis? I would except it to move along forward and rotate around up.
Anyhow: It is probably easier to use Quaternion.LookRotation than calculate the values yourself. The following code should do the same as your code:
Vector3 toPlayerVec = player.transform.position - this.transform.position;
toPlayerVec.z = 0.0f;
Quaternion q = Quaternion.LookRotation(toPlayerVec, Vector3.forward)*Quaternion.Euler(270,180,0);
I just simply had to change the axis so that based on the sprite it would face the right way without editing all the sprite models.
Answer by Rasgard · Jun 26, 2016 at 04:02 PM
Could someone please modify the script so that I could test it for the bug. Thank you.
I am not sure what you mean. Did replacing
float z = $$anonymous$$athf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * $$anonymous$$athf.Rad2Deg - 90;
Quaternion q = Quaternion.AngleAxis(z, transform.forward);
with the code I posted make a difference?
Thanks for your post however this does insta rotation. I needed it to rotate slowly towards the player. I had a similar script before that while it does not have weird bugs it is not what I am looking for exactly.
Sorry I just did not realize what to replace exactly. The code works however upon collision the vector still gets messed up a little. So the enemy would try to face me sideways sometimes. This is more noticeable with slow rotation in place.
I think I figured it out, my Rigidbody2D had 0 Angular Drag. Combine that with no gravity, I had physics forces contantly affecting the object when I hit it, thus causing it to go crazy and face me with its wrong sides. Thank you ChrisAngel$$anonymous$$indmapfreak.