Advertisement

Trying to rotate an object in unity

Started by December 03, 2018 05:34 AM
6 comments, last by Tom Sloper 5 years, 11 months ago

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

 

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 ?

Check out my game '3NDL3ZZ: Base Defense' A simple militant styled, tower defense game.

GameDev.Net Project Page

Advertisement
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 :)

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?

Check out my game '3NDL3ZZ: Base Defense' A simple militant styled, tower defense game.

GameDev.Net Project Page

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!

 

 

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!

Check out my game '3NDL3ZZ: Base Defense' A simple militant styled, tower defense game.

GameDev.Net Project Page

Advertisement
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.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement