如何限制transform.Rotate(float,0,0)?

时间:2019-02-17 15:50:31

标签: c# unity3d

我知道Google中有一些答案,但是我不能用以下代码来限制轮换:

element {pointer-events:auto}

我希望在 X 轴上的范围从 0 90

谢谢!

1 个答案:

答案 0 :(得分:0)

使用四元数代替。

public Vector3 rotation;
private GameObject go;

private void Start()
{
    go = GameObject.CreatePrimitive(PrimitiveType.Cube);
}

void Update()
{
    go.transform.rotation = Quaternion.Euler(Mathf.Clamp(rotation.x, 0, 90), 
    rotation.y, rotation.z);
}

public class CameraClamp : MonoBehaviour
{
    public float speed;
    public Vector2 clamp; // x = min, y = max
    private float pitch;

    private void Update()
    {
        pitch += Input.GetAxis("Mouse Y") * speed * Time.deltaTime;
        //pitch -= Input.GetAxis("Mouse Y") * speed * Time.deltaTime; // Invert
        pitch = Mathf.Clamp(pitch, clamp.x, clamp.y);
    }

    private void LateUpdate()
    {
        gameObject.transform.rotation = Quaternion.Euler(pitch, 0, 0);
    }
}