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°)
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:
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:
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);