- Home /
Question by
bandirstfm · Oct 13, 2020 at 08:11 PM ·
c#rotationscripting problem
Hi! My question would be this: How can I limit the rotation of an object, because it seems like the Mathf.Clamp doesn't work have I wanted it. I want it on the x and on the z axis (Im new to Unity))
using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Runtime; using UnityEngine;
public class swordrotation : MonoBehaviour {
public float mouseSensitivity = 100f;
public Transform SwordRotation;
float xRotation = 0f;
float zRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation = Mathf.Clamp(xRotation, -40, 60);
zRotation = Mathf.Clamp(zRotation, -90, 90);
SwordRotation.Rotate(new Vector3(10f, 0f, 0f) * mouseY);
SwordRotation.Rotate(new Vector3(0f, 0f, 10f) * mouseX);
}
}
}
Comment