【问题标题】:Unity | Rotate the gameObject by specified angle (0~360 degree)团结 |将游戏对象旋转指定角度(0~360 度)
【发布时间】:2015-05-20 08:09:01
【问题描述】:

我正在努力用操纵杆旋转游戏对象。摇杆将 json 数据包含角度的值发送给游戏对象。 gameOjbect 应该在接收到 Jsondata 时自行旋转。但是,我想知道如何将它统一旋转角度(0 到 360 度),因为我所知道的只是使用下面的 (Vector3) 位置。

Quaternion.LookRotation
public static Quaternion LookRotation(Vector3 forward, 
                                      Vector3 upwards = Vector3.up);

总之,我只想知道将游戏对象旋转一定角度。

【问题讨论】:

    标签: c# unity3d rotation angle quaternions


    【解决方案1】:

    使用RotateAround

    // Rotate around world y.
    transform.RotateAround(transform.position, Vector3.up, angle);
    
    // Rotate around local y.
    transform.RotateAround(transform.position, transform.up, angle);
    

    无论如何,您可能会在 Transform 文档中找到其他有用的东西。

    【讨论】:

    • 谢谢!!很抱歉回复晚了:-
    • 本地向上并不总是向上。如果一个立方体正在“看着”地面,那么它的本地向上与世界向上根本不同(与左上角的 Unity 编辑器本地/全局开关相同)。您从触摸中获得什么,以及如何将其应用到哪个视图方向……完全取决于您的实施。我建议在播放模式下手动调整 GameObject 变换(这是 Unity 最擅长的),然后确定你想要驱动的值/角度。之后,您可以弄清楚您想如何驾驶它。
    【解决方案2】:
    transform.eulerAngles = new Vector3(90, 0, 0);
    

    将您的游戏对象在 x 轴上旋转 90 度。

    或者你可以平滑地旋转

    Vector3 destination = new Vector3(90,0,0);
    transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, 
                                         destination, 
                                         Time.deltaTime);
    

    【讨论】:

      【解决方案3】:

      给正在阅读本文的人的快速提示。 transform.RotateAround 在 Unity 的最新版本中已过时。需要改用transform.Rotate(Vector3 eulerAngles)

      这是一个实例化以“randomAngleRotation”角度旋转的射弹的示例。

      Projectile newProjectile = Instantiate<Projectile> (projectile,projectileSpawn [i].position, projectileSpawn [i].rotation) as Projectile;
      newProjectile.transform.Rotate(new Vector3(Random.Range(randomAngleRotation, randomAngleRotation), Random.Range(randomAngleRotation, randomAngleRotation)));
      

      【讨论】:

        猜你喜欢
        • 2017-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-10
        相关资源
        最近更新 更多