[英]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.