rotate a plane as long as if statement is true?
hi! i want to rotate a plane as long as the mouse raycasted a object. which looks like this now:
if (Input.GetMouseButton(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 == "Trigger") {
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, 0.15f);
}
}
}
and if you release the left button it should rotate back to zero:
if (!Input.GetMouseButton(0)){
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, 0.15f);
}
so what i want to understand or like to know is, how can i let the plane rotate as long as the mouse button is down smoothly until a certain degree? (lets say 20°)
Answer by callen · Dec 10, 2015 at 03:08 PM
I hope this helps, but I don't fully understand what you're trying to do with that code. In both your mouse-down and mouse-up blocks, you use the same rotation code:
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, 0.15f);
This is mostly correct for when you want to return to zero rotation (that's what the Identity will give you), but to end somewhere else, say 20 degrees (along the z-axis), you could do something like this:
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0,0,20), 0.15f);
But for your constantly-rotating logic, using Lerp isn't the best option. Instead, you would want to multiply the current rotation with another quaternion representing a small arc of the desired rotation, like this:
//rotate the object one full revolution per second, around the z-axis
transform.rotation *= Quaternion.Euler(0,0,360*time.deltaTime);
Hope this helps!
thanks! the second one was what i was looking for :) sorry for the weird explaination in the original post :)
wait, actually i have another question: with
transform.rotation *= Quaternion.Euler(0,0,20*Time.deltaTime);
it rotates smoothly but all the time. but with
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0,0,20), 0.15f);
it rotates til 20° but not smooth. how can i let it rotate smooth and with this limit?
What the second line there is doing, is every frame it rotates 15% of the way between the current rotation and the 20 degree rotation. So for example, if you were starting at 120 degrees...
Frame 1, rotate (120 - 20) * 0.15 = 15 degrees, and your new rotation is 105
Frame 2, rotate (105 - 20) * 0.15 = 12.75 degrees, and new rotation is 92.25
Frame 3, rotate (92.25 - 20) * 0.15 = 10.84 degrees, and new rotation is 81.41
...
This will keep bringing the angle towards 20, but each frame the angle rotated becomes smaller and smaller. After many frames the equation will look something like: Frame X, rotate (20.05 - 20) * 0.15 = 0.075 degrees, new rotation is 20.425
Now, with the 20*Time.deltaTime version, you're essentially (if the framerate is constant) moving the object the same angle each frame. The constant you multiply it by (20 in your example) means it will rotate that many degrees per second, whereas in my example I used 360 to give one full rotation per second. Whatever speed works best for you.
If what you're really after is a combination of these two - aka to move at a constant speed, until you get to a certain stop-point, you would use the constant-motion version and add a check along the lines of
//declare a member variable named 'lastAngle' in the class
transform.rotation *= Quaternion.Euler(0, 0, 360 * Time.deltaTime);
if(lastAngle < 20 && transform.rotation.eulerAngles.z >= 20)
enabled = false; //or another piece of logic to turn off the spinning effect
lastAngle = transform.rotation.eulerAngles.z;
However, note that this approach has lots of gotchas - if your 'target' is small or 0, the "lastAngle < target" part might never be true. Also, your final angle may be slightly larger than the target (like, 20.03)... you should be able to get your basics working with all this though!
Your answer
![](https://koobas.hobune.stream/wayback/20220612052106im_/https://answers.unity.com/themes/thub/images/avi.jpg)