首页 > 解决方案 > Unity中的夹具旋转问题

问题描述

我试图限制我的 x 轴旋转,但它根本不夹紧。就像没有效果一样。我究竟做错了什么?

    public float speed = 5f;
    public float minRotation = -45;
    public float maxRotation = 10;

    void Update()
    {
       
        if (Input.GetMouseButton(0))
        {
   
           float xaxisRotation = Input.GetAxis("Mouse Y") * speed;
           xaxisRotation = Mathf.Clamp(xaxisRotation, minRotation, maxRotation);
           transform.Rotate(Vector3.right, xaxisRotation);
  
        }
    }

标签: c#unity3d

解决方案


您钳制每帧应用的最大旋转。因此,如果您想钳制旋转本身,则必须钳制总旋转。

尝试:

public float speed = 5f;
public float minRotation = -45;
public float maxRotation = 10;

void Update()
{
   
    if (Input.GetMouseButton(0))
    {

       float xaxisRotation = Input.GetAxis("Mouse Y") * speed;
       transform.Rotate(Vector3.right, xaxisRotation);
       transform.y = Mathf.Clamp(transform.eulerAngles.y, minRotation, maxRotation);

    }
}

推荐阅读