繁体   English   中英

团结| 将游戏对象旋转指定角度(0〜360度)

[英]Unity | Rotate the gameObject by specified angle (0~360 degree)

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM