Jump to content
  • Advertisement
Sylon87

Trying to rotate an object in unity

Recommended Posts

hello,
What i’m trying to do is to rotate my object pvot from -180 to -180, so a full rotation, actually i have this
 

IEnumerator Rotate(float duration)
 {
     float startRotation = transform.eulerAngles.y;
     float endRotation = startRotation + 360.0f;
     float t = 0.0f;
     while ( t  < duration )
     {
         t += Time.deltaTime;
         float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, 
         transform.eulerAngles.z);
         yield return null;
     }
 }

But after some degrees it will rotate in a wrong direction, i suppose because quaternion works with x,y anyone can help me to fix it?

thank’s u in advance

 

Share this post


Link to post
Share on other sites
Advertisement

I think you want to use RotateAround()

 https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

 GameObject go;
 float angle = 360.0f; // Degree per time unit
 float time = 1.0f; // Time unit in sec
 Vector3 axis = Vector3.up; // Rotation axis, here it the yaw axis
 
 private void Update()
 {
     go.GetComponent<Transform>().RotateAround(Vector3.zero, axis, angle * Time.deltaTime / time);
 }

I found the above but its from 2014 so I don't know exactly how upto date it is.. but maybe that helps? I'm learning Unity so I don't know it all yet 😂

Share this post


Link to post
Share on other sites
14 minutes ago, CyberFlash said:

I think you want to use RotateAround()

 https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html


 GameObject go;
 float angle = 360.0f; // Degree per time unit
 float time = 1.0f; // Time unit in sec
 Vector3 axis = Vector3.up; // Rotation axis, here it the yaw axis
 
 private void Update()
 {
     go.GetComponent<Transform>().RotateAround(Vector3.zero, axis, angle * Time.deltaTime / time);
 }

I found the above but its from 2014 so I don't know exactly how upto date it is.. but maybe that helps? I'm learning Unity so I don't know it all yet 😂

Hi!

thank’s you for your reply, but rotatearound is if you want that object never stop rotating,  what i want is that it will stop exactly on 180 degrees :)

Share this post


Link to post
Share on other sites

What about RotateTowards?

go.RotateTowards(go.rotation, Quarternion.Euler(0,180,0), 1);

 

I was wondering, could you not make it as an animation? And then just play the animation when you want to activate the rotation?

Share this post


Link to post
Share on other sites

It’s for my school, so i must do it without animation, 

 

thank’s you for the rotatetowards , i completly forget about it, i’ll try it tomorrow and let you know if works!

 

 

Share this post


Link to post
Share on other sites

Ahh I see, its for an assignment. Fair enough, I'm learning it all myself so I could be wrong D:, I know there's some wayyyy smarter folks on here that can give better answers! I hope it works though!

Share this post


Link to post
Share on other sites
On 12/3/2018 at 5:35 AM, Sylon87 said:

It’s for my school

Sorry, Sylon. The gamedev.net policy is no homework helping here. Locking thread.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!