簡體   English   中英

C#,Unity和旋轉

[英]C#, Unity, and rotation

我想模仿一條隨機蜿蜒的蛇。 這在Update()中,但是旋轉並不是我想要的。

我的游戲對象的y旋轉始於270度。 乍一看似乎可行,但總會得出結論。 360度。

float currentY = transform.rotation.y;

// the initial y rotation starts at 270
// seems like it rotates y to be between 350 and 10 degrees only.
// This is not what I want. I want it to randomly turn a little to the left or right,
// and then move forward
transform.rotation = Quaternion.Lerp(
    transform.rotation, 
    Quaternion.Euler(0f, (Random.Range(-10.0f, 10.0f) + currentY), 0f), 
    (float)(Time.time * .01)
);


rbody.AddForce(transform.forward * speed * 2.5f);

你快到了 以下應該工作:

float currentY = transform.rotation.y;

// Randomly rotate +- 10 degrees on the y axis
var rotateBy = Random.Range(-10.0f, 10.0f);
transform.rotation = Quaternion.Lerp(
    transform.rotation, 
    Quaternion.Euler(0f, rotateBy, 0f), 
    (float)(Time.time * .01)
);


rbody.AddForce(transform.forward * speed * 2.5f);

以前,您要求它將y軸旋轉y +- 10 您真正想要做的是將y軸旋轉+- 10 ,而不添加當前y值。

那是; 你假設它會旋轉指定的角度,但是它會指定角度旋轉。

好吧,她昨天知道了。 正如某人所說,我認為那不是一個角度,但我認為她意識到自己。 謝謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM