亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 RotateAround 旋轉對象并給它一個限制

使用 RotateAround 旋轉對象并給它一個限制

Go
喵喔喔 2022-11-21 20:15:10
我的場景中有一個對象可以通過鼠標滑動旋轉 ( RotateAround )。我想給對象一些旋轉限制,例如 X 軸的 -45 和 45 度,所以當它的旋轉變成 45 度時,它不能超過它。所以我在我的腳本中嘗試了 Mathf.Clamp方法,如下所示,它在 Y 軸上工作正常,對象圍繞他的 X 軸旋轉并且沒有超出 Y 限制。但在 X 軸上,當物體的 Y 旋轉達到 O 時,它會立即變為 30 度,并出現奇怪的旋轉!你能告訴我的代碼有什么問題嗎?旋轉腳本:float sensitivity = 10f;Vector3 firstPressPos;Vector3 secondPressPos;float minRotationX = 45;float maxRotationX = 100;float minRotationY = 30;float maxRotationY = 30;void Update () {    if (Input.GetMouseButtonDown(0))    {        //save began touch 2d point        firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);    }    if (Input.GetMouseButton(0))    {        //save ended touch 2d point        secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);        if (firstPressPos != secondPressPos)        {            float RotX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;            float RotY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;            transform.RotateAround(Vector3.up, RotX);            transform.RotateAround(Vector3.right, -RotY);            Vector3 angles = transform.eulerAngles;            angles.x = Mathf.Clamp(angles.x, minRotationX, maxRotationX);            angles.y = Mathf.Clamp(angles.y, -minRotationY, maxRotationY);            angles.z = 0;            transform.eulerAngles = angles;        }    }}
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

在編輯器中,旋轉值介于 -180 和 180 之間,但在 transform.eulerAngles 中,它們實際上介于 0 和 360 之間。


所以你需要在夾緊它之前調整你的角度值。


if(angles.y > 180)

{

    angles.y -= 180f;

}


angles.y = Mathf.Clamp(angles.Y, minY, maxY);


查看完整回答
反對 回復 2022-11-21
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號