【问题标题】:Rotate object towards another object - C# Unity将对象旋转到另一个对象 - C# Unity
【发布时间】:2015-03-13 07:45:49
【问题描述】:

我希望物体在生成时转向我的敌方物体。我已经完成了旅行,但朝向敌人的旋转似乎不起作用。

这是我当前的代码:

GameObject newRocket = GameObject.FindGameObjectWithTag("Enemy");
direction = (cil.transform.position - novaStrela.transform.position);
rotationDirection = Quaternion.LookRotation(direction);
newRocket = Instantiate (rocket, transform.position, rotationDirection) as GameObject;
newRocket.rigidbody.AddForce ((target.transform.position - transform.position).normalized * projectileSpeed);

我知道这有点乱,但如果你能帮助我,我会很高兴。到目前为止谢谢你

【问题讨论】:

  • 我假设对象“cil”是敌人,而“novaStrela”是玩家/射手?
  • 发布一张图片,将结果与“rotationDirection”与“Quaternion.identity”作为旋转参数进行比较。
  • @ThisHandleNotInUse 是的,你猜对了..我没有纠正这个..所以 cil = target 和 novaStrela = newRocket....

标签: c# unity3d rotation projectile


【解决方案1】:

试试这个:

Vector3 v3Dir = transform.position - _destination.position;
            float angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
            transform.eulerAngles = new Vector3(0, 0, angle);

其中transform.position 是火箭的位置(您要查看的对象),_destination.position 是要查看的对象的位置。您可能需要摆弄,因为这是我的 2D 游戏代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-13
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多