跟随我的玩家的统一敌人精灵旋转

Unity enemy sprite rotation following my player

使用 unity 5.4,top down 2D 游戏。

我的坦克敌人精灵有 4 个不同的脚本。一个移动并瞄准我的玩家,一个射击,另一个跟随玩家并最后一个控制 HP.The 问题的伤害处理程序脚本是当我玩游戏时我的精灵对角线移动或面向我的玩家的相反方向。 (见截图)。我的精灵一直固定在播放器上。

我的跟随我的玩家的脚本是

using UnityEngine;
using System.Collections;

public class FollowPlayer : MonoBehaviour {

public float rotSpeed = 90f;
Transform player;

void Update () {
    if(player == null)
    {
        GameObject go = GameObject.Find("Player");

        if(go != null)
        {
            player = go.transform;
        }
    }
    if (player == null)
        return;

    Vector3 dir = player.position - transform.position;
    dir.Normalize();

    float zAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

     Quaternion desiredRot= Quaternion.Euler(zAngle, 0, 0);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRot, rotSpeed * Time.deltaTime);

}}

我认为我的精灵向玩家移动的问题是

代码的一部分
float zAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

我不相信我完全理解这句话的全部意思。或者它太复杂了。

进一步说明。我的坦克精灵必须跟随我的玩家(在任何时候,直到他们压垮我),炮塔必须一直面向我的玩家,因为他们向我射击。我的子弹朝正确的方向发射,但我的 sprite 正确地朝向我的玩家移动但面向另一个方向。

不同的精灵可能有不同的前锋位置。添加一个 public float rotationFix = 90f; 然后靠近底部:float zAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + rotationfix; 并用它来调整有效旋转和 'fix' 精灵的前向位置。

可以在游戏过程中通过检查器轻松完成调整。