- Home /
problem with object rotation
for the auxiliary UI Button, I visit this line of code, how can I make the object click on the button turn 75 degrees and when he press the button again it returns to its original coordinates, that is, 0, I can’t find how to do it
if (click_detail4_1 == true)
{
detail4_1.localRotation = Quaternion.RotateTowards(detail4_1.localRotation, Quaternion.Euler(new Vector3(0, 75, 0)), 40f * Time.deltaTime);
}
if (click_detail4_2 == true)
{
detail4_2.localRotation = Quaternion.RotateTowards(detail4_2.localRotation, Quaternion.Euler(new Vector3(0, -75, 0)), 40f * Time.deltaTime);
}
Is there a reason you are using localRotation ins$$anonymous$$d of transform.rotation?
Yes, and this is a very important part
Answer by Batzuga · Jan 28, 2020 at 01:33 AM
A rather simple way is to lerp one float value, which will be the axis you want to rotate around, and set the rotation with eulerAngles.
GameObject rotationObject;
float axisrotation = 0;
float targetrotation = 0;
void Update()
{
axisrotation = Mathf.Lerp(axisrotation, targetrotation, 1.0f * Time.deltaTime);
rotationObject.transform.eulerAngles = new Vector3(0, axisrotation, 0);
}
public void Click()
{
if (targetrotation == 0)
{
targetrotation = 75;
return;
}
if(targetrotation == 75)
{
targetrotation = 0;
}
}
Your answer
Follow this Question
Related Questions
Rotate an object to a given point 2 Answers
How to turn the steering wheel 0 Answers
How to assign a rotation relative to another object? 2 Answers
turning a project using AddTorque 0 Answers
what are the ways to attach objects 1 Answer