Limit Rotation around z axis with mouse drag
trying to limit rotation of an object on the wall like a door knob, i can rotate perfectly fine but id like to limit the rotation can anyone help please
much appreciated
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class rotRoomStat : MonoBehaviour {
public float rotSpeed = 20;
// Use this for initialization
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
transform.RotateAround(Vector3.forward, -rotX);
}
}
Answer by Jawchewa · Jun 08, 2017 at 08:36 PM
I would recommend using the "Clamp" function to limit the rotation angle. See this link for more information about how that works:
https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
Basically, it would look something like this:
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.Deg2Rad;
rotX = (Mathf.Clamp(rotX, -45.0F, 45.0F);
transform.RotateAround(Vector3.forward, -rotX);
but with the 45s replaced with whatever you want to use for your range.
that doesn't clamp my rotation for some strange reason
dude, When I used this thing it gives me error cs1026 ı am trying the same thing but at rotX....
code is here :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pick : $$anonymous$$onoBehaviour
{
public float RotationSpeed = 5;
void Update ()
{
float rotX = Input.GetAxis("$$anonymous$$ouse X")*rotSpeed*$$anonymous$$athf.Deg2Rad; //tryng to limit limitation
rotX = ($$anonymous$$athf.Clamp(rotX, -45.0f, 45.0f); //before semicon it says there must be an } ( why?).
transform.RotateAround(Vector3.forward, -rotX); //tryng to limit limitation
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}
}
Answer by gamedivision · Jun 10, 2017 at 09:17 PM
still struggling with this if anyone can offer an alternative
Your answer
Follow this Question
Related Questions
ROTATION STOPS when i move the char on the air! 0 Answers
have one object rotate another object on the y axis 0 Answers
Rotate an Object while moving forward using empty gameObject 0 Answers
How to object rotate on a single axis towards another object? C# 2 Answers
How to rotate an object according to the camera's view? 1 Answer