【问题标题】:Get Axis relative to rotation Unity3d获取相对于旋转Unity3d的轴
【发布时间】:2018-09-27 23:18:44
【问题描述】:

我目前正在使用 Unity 和 Photon 开发一款“格斗”风格的游戏。但是,我遇到了一个小问题,我不知道如何解决。

游戏是 1 对 1,因此房间内一次最多只能有两个玩家。我有一个带有健康栏和弹出损坏文本的广告牌效果。例如,当玩家旋转相机时,敌人的文本/生命条将旋转以注视相机。

问题在于损坏文本。它的作用是当敌人被击中时会出现正确的数字并且它面向正确的方向,但是,我想在生成位置添加一个随机变量,这样它就不会在同一个地方生成。发生的情况是轴会根据对象面向的方向(看着玩家)发生变化。所以换句话说,如果我想要一个随机化的 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);
    }

}

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

【问题讨论】:

  • 直接沿 x 轴改变它只会影响它在世界空间中的 x 轴。我建议使用 transform.right 来获得正确的向量来进行转换,因为它将考虑到文本的旋转

标签: c# unity3d rotation position photon


【解决方案1】:

试试这个

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()返回int

【讨论】:

  • 我现在不在家(应用程序),但是,我正在测试它的路上。我会告诉你的。
  • 对不起,我对堆栈溢出很陌生。谢谢你告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多