获取相对于旋转Unity3d的轴

Get Axis relative to rotation Unity3d

我目前正在使用 Unity 和 Photon 开发 "Fighting" 风格的游戏。但是,我 运行 陷入了一个我不知道如何解决的小问题。

游戏为 1v1,因此同一时间房间内最多只能有两名玩家。我有一个带有健康栏和弹出伤害文本的广告牌效果。例如,当玩家旋转相机时,敌人 text/healthbar 将旋转以注视相机。

问题出在损坏文本上。它的工作原理是当敌人被击中时会出现正确的数字并且它面向正确的方向,但是,我想在生成位置添加一个 运行dom 变量,这样它就不会同时生成地方。发生的事情是轴根据对象所面对的方向(看着玩家)而变化。所以换句话说,如果我想要一个 运行domize x 轴,它会根据对象的旋转更改为 y 和 z。

这里有一些图片可以帮助描述:

问题:

这是我正在使用的代码,它附在角色上:

void ShowFloatingText(int amt)
{
    Transform c = transform.Find("DamageSpawn").transform;
    GameObject go = PhotonNetwork.Instantiate("DamageText", transform.position, Quaternion.identity, 0, null);
    go.transform.SetParent(this.transform);
    go.transform.localPosition = c.localPosition;
    go.transform.localPosition += new Vector3(Random.Range(-randomizeInt.x, randomizeInt.x), Random.Range(-randomizeInt.y/2, randomizeInt.y/2), 0);

    go.GetComponent<TextMesh>().text = amt.ToString();
}

这是附加到 damageText 对象并控制旋转:

 public void GetCamera()
 {
    GameObject[] g = GameObject.FindGameObjectsWithTag("Player");
    foreach (GameObject gg in g)
    { 
        if (gg.gameObject.layer == LayerMask.NameToLayer("isSelf"))
        {
            cam = gg.transform.Find("FirstPersonCharacter").GetComponent<Camera>();


        }
    }

}
private void Start()
{
    Destroy(gameObject, 1.5f);
    //transform.position += offset;
    GetCamera();

}
void Update()
{
    if (cam == null) 
    {
        GetCamera();
    }
    else
    {
        transform.LookAt(transform.position + cam.transform.rotation * Vector3.forward, cam.transform.rotation * Vector3.up);
    }

}

我将不胜感激并帮助解决这个问题。 谢谢

试试这个

void ShowFloatingText(int amt)
{
    Transform c = transform.Find("DamageSpawn").transform;
    GameObject go = PhotonNetwork.Instantiate("DamageText", transform.position, Quaternion.identity, 0, null);
    go.transform.SetParent(this.transform);
    go.transform.localPosition = c.localPosition;
    go.transform.localPosition += go.transform.right * Random.Range(-randomizeInt.x/2, randomizeInt.x/2);
    go.transform.localPosition += new Vector3(0, Random.Range(-randomizeInt.y/2, randomizeInt.y/2), 0);

    go.GetComponent<TextMesh>().text = amt.ToString();
}

这考虑了变换的旋转并沿那个向量平移,而不是简单地沿世界空间x轴移动游戏对象,假设Random.Range( ) returns 一个 int.