- Home /
I wanna use rotateAround but up to 45 degrees
I wanna rotate my object around an edge object but it doesnt stop. its x rotation reach 180 and move on like -179, -178.. and make a circle like this. But I want to rotate it up to 45 degrees and back to 0 degrees again in a loop. here is my code part ; obj.transform.RotateAround(edgePoint.transform.position, vec, 20 * Time.deltaTime);
matf.pingpong is used in Quaternion.Euler() like obj.transform.position = new Vector3(Mathf.PingPong(Time.time * 2, max - $$anonymous$$) + $$anonymous$$, obj.transform.position.y, obj.transform.position.z); but it rotates whole object. I need the rotateAround version of it but I am so new at unity and couldnt figure out how to do it :(
Answer by DrTomato · Mar 30, 2021 at 05:36 PM
You normally need to change the pivot point in a 3D modelling application to have your model to rotate around a certain point.
Alternatively, in Unity you can create an empty game object and position it at the point you want to rotate around and then make the object you want to rotate a child of the new empty object. Now rotate the parent object using Quaternion.Lerp or if you want to use euler angles there is a solution here https://answers.unity.com/questions/643141/how-can-i-lerp-an-objects-rotation.html
Edit: The link wasn't intended as a full answer but to direct you to information to piece together a solution yourself. I have included a more direct solution below.
IEnumerator LerpRotation(Vector3 rotationAmount, float duration)
{
print("Start");
float time = 0;
Vector3 startRotation = transform.rotation.eulerAngles;
Vector3 targetRotation = startRotation + rotationAmount;
while (time < duration)
{
print(Vector3.Lerp(startRotation, targetRotation, time / duration));
transform.rotation = Quaternion.Euler(Vector3.Lerp(startRotation, targetRotation, time / duration));
time += Time.deltaTime;
yield return null;
}
time = 0;
while (time < duration)
{
transform.rotation = Quaternion.Euler(Vector3.Lerp(targetRotation, startRotation, time / duration));
time += Time.deltaTime;
yield return null;
}
transform.position = startRotation;
print("End");
}
This can be started using
StartCoroutine(LerpRotation(new Vector3(0, 45, 0), 1));