limit Quaternion.Euler rotation
hey! so i got this piece of code when a button gets hold down:
maze.transform.rotation *= Quaternion.Euler(0,0,40*Time.deltaTime);
with this script snippet a plane is rotating smoothly all the time. but i want to let it stop at lets say 20° even if my button is still gets hold down. how can i do this? thanks!
I'm going to leave this as a comment, because I'm not entirely proficient in this, but have you tried the $$anonymous$$athf.Clamp() method to limit the rotation yet? I believe you can just do this:
maze.transform.rotation *= Quaternion.Euler(0,0,$$anonymous$$athf.Clamp(40*Time.deltaTime, -20, 20));
thanks. but that also let the plane rotate infinite
Answer by akh32 · Dec 11, 2015 at 01:56 PM
I would start by storing your result in a temporary variable, then testing the z-rotation, like so:
Quaternion t = maze.transform.rotation * Quaternion.Euler(0,0,40*Time.deltaTime);
if(t.euler.z > 20){
t = Quaternion.Euler(t.euler.x, t.euler.y, 20);
}
maze.transform.rotation = t;
This assumes that you want a global limit of 20° rotation, and not that you want the maximum amount of rotation provided by this button to be 20°.
Answer by mr_duke · Dec 10, 2015 at 05:23 PM
mby
maze.transform.rotation = Quaternion.RotateTowards(maze.transform.rotation, Quaternion.Euler(0,0,40), Time.deltaTime);
help
thanks. but that also let the plane rotate infinite this is the whole code if that helps:
void Update() {
if (Input.Get$$anonymous$$ouseButton(0)) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100)) {
// whatever tag you are looking for on your game object
if(hit.collider.tag == tag) {
maze.transform.rotation *= Quaternion.Euler(yrotation*Time.deltaTime,0,xrotation*Time.deltaTime);
Debug.Log("---> Hit: ");
}
}
}
private const float STOP_ANGLE = 20;
private const float SPEED_ANGLE = 180;//per second
public GameObject maze;
private float mazeYangle = 360;//curren angle
RaycastHit hit;
private void Update()
{
if ( Input.Get$$anonymous$$ouseButton( 0 ) && Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hit, 100 ) )
{
var oldAngle = mazeYangle;
mazeYangle = ( mazeYangle + SPEED_ANGLE * Time.deltaTime - STOP_ANGLE ) % 360 + STOP_ANGLE;
maze.transform.Rotate( 0, mazeYangle - oldAngle , 0 ) ;
} else
{
var oldAngle = mazeYangle;
mazeYangle = $$anonymous$$athf.$$anonymous$$in( mazeYangle + SPEED_ANGLE * Time.deltaTime - STOP_ANGLE, 360 ) + STOP_ANGLE;
maze.transform.Rotate( 0, mazeYangle - oldAngle, 0 );
}
}
Your answer
Follow this Question
Related Questions
Rotating Vector3, linecast 1 Answer
unwanted rotation in y axis 0 Answers
How can I set rotation properly? 0 Answers
Rotation problem 0 Answers
transform rotation inaccuracy 0 Answers