简体   繁体   English

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

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

I'm struggling to rotate gameObject with joystick. 我正在努力用操纵杆旋转gameObject。 The joystick send the json data included value of angle to gameObject. 游戏杆将json数据包含的角度值发送到gameObject。 The gameOjbect should rotate itself when receive the Jsondata. 收到Jsondata时,gameOjbect应该自行旋转。 However, i'm wondering how to rotate it by angle (0 to 360 degree) in unity because all i do know is using (Vector3) position below. 但是,我想知道如何统一旋转角度(0到360度),因为我所知道的就是使用下面的(Vector3)位置。

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

In conclusion, all i want to know is rotating the gameObject by the angle. 总之,我想知道的是将游戏对象旋转一定角度。

Use RotateAround . 使用RotateAround

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

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

You may found other useful stuff in Transform documentation anyway. 无论如何,您可能会在Transform文档中找到其他有用的东西。

transform.eulerAngles = new Vector3(90, 0, 0);

Rotates your gameobject to 90 degrees in x axis. 将您的游戏对象在x轴上旋转90度。

Or you can rotate smoothly with 或者,您可以使用

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

Quick tip for whoever is reading this now. 凡是正在阅读此书的人都可以快速获得提示。 transform.RotateAround is outdated in Unity's latest version. transform.RotateAround在Unity的最新版本中已过时。 Need to use transform.Rotate(Vector3 eulerAngles) instead. 需要使用transform.Rotate(Vector3 eulerAngles)代替。

Here is an example to instantiate projectiles rotated on 'randomAngleRotation' angles. 这是一个实例化以'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