Unity |旋转游戏对象指定的角度(0~360度)

本文关键字:360度 旋转 游戏 对象 Unity | 更新日期: 2023-09-27 17:50:01

我很难用操纵杆旋转gameObject。操纵杆将包含角度值的json数据发送给gameObject。gameobject应该在接收到Jsondata时旋转自己。然而,我想知道如何旋转它的角度(0到360度)在统一,因为我所知道的是使用(Vector3)下面的位置。

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

总之,我想知道的是旋转gameObject的角度。

Unity |旋转游戏对象指定的角度(0~360度)

使用RotateAround

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

你可以在Transform文档中找到其他有用的东西。

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);

给正在读这篇文章的人一个小提示。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)));