• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Anykey · Jul 27, 2011 at 07:49 PM · rotationtransformrotateeuleranglesbce0017

Rotate and eulerAngles don't work together ??

Hi ! I've encountered an error when changing rotation into Rotate in front of euler angles

 var Statue : GameObject;
 
 var MinClamp1 = 0f;
 var MaxClamp1 = 90f;
 var MinClamp2 = 90f;
 var MaxClamp2 = 180f;
 var MinClamp3 = 180f;
 var MaxClamp3 = 270f;
 var MinClamp4 = 270f;
 var MaxClamp4 = 360f;
 var RotationLimit = 0;
 function Update () {
 if (Statue.transform.rotation.y > 360){
 Statue.transform.rotation.y = 0;
 }
 if (RotationLimit == 4){
 RotationLimit = 0;
 }
 }
 
 
 function OnTriggerStay(other : Collider){
 if (other.gameObject.tag == "Boite" && RotationLimit == 0){
   Statue.transform.rotation.eulerAngles =  new Vector3(Statue.transform.rotation.eulerAngles.x,Mathf.Clamp(Statue.transform.rotation.eulerAngles.y, MinClamp1, MaxClamp1),
           Statue.transform.rotation.eulerAngles.z);
           }
           if (other.gameObject.tag == "Boite" && RotationLimit == 1){
   Statue.transform.rotation.eulerAngles =  new Vector3(Statue.transform.rotation.eulerAngles.x,Mathf.Clamp(Statue.transform.rotation.eulerAngles.y, MinClamp2, MaxClamp2),
           Statue.transform.rotation.eulerAngles.z);
           }
           if (other.gameObject.tag == "Boite" && RotationLimit == 2){
   Statue.transform.rotation.eulerAngles =  new Vector3(Statue.transform.rotation.eulerAngles.x,Mathf.Clamp(Statue.transform.rotation.eulerAngles.y, MinClamp3, MaxClamp3),
           Statue.transform.rotation.eulerAngles.z);
           }
           if (other.gameObject.tag == "Boite" && RotationLimit == 3){
   Statue.transform.rotation.eulerAngles =  new Vector3(Statue.transform.rotation.eulerAngles.x,Mathf.Clamp(Statue.transform.rotation.eulerAngles.y, MinClamp4, MaxClamp4),
           Statue.transform.rotation.eulerAngles.z);
           }
 }
 function OnTriggerExit(other : Collider){
 ++RotationLimit;
 }

it works , but I'd like to make it rotate slowly like using Rotate instead of rotation , how should I do ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Jul 29, 2011 at 01:35 PM

I had no time to test this, but I think it does exactly what you're trying to do - replace your script with this one and tell me if you have any problem:

var Statue : GameObject; private var rotating = false;

function OnTriggerEnter(other : Collider){ if (other.gameObject.tag == "Boite"){ RotateY(Statue.transform, 90, 1.1); // rotate gradually 90 around Y } }

function RotateY(transf: Transform, angle: float, time: float){ if (rotating) return; // ignore other calls while rotating rotating = true; // flag to indicate "I'm rotating already" var t: float = 0; var euler = transf.eulerAngles; var init = euler.y; var end = init + angle; while (t < 1){ // t will vary from 0 to 1 in time seconds t += Time.deltaTime / time;
euler.y = Mathf.Lerp(init, end, t); // define Y angle proportional to t transf.eulerAngles = euler; yield; // return here in the next frame } rotating = false; // finished rotating }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anykey · Jul 29, 2011 at 03:04 PM 0
Share

Thank you so much aldonaletto , it couldn't work better ! If you need 3D $$anonymous$$odeling help , tell me here : DanaoPolice(at)hotmail.fr , I would be glad to help you in return with what I know doing ;)

avatar image aldonaletto · Jul 29, 2011 at 05:05 PM 0
Share

Thanks for your offer - be sure I'll cry for help if I need!

avatar image
1

Answer by almo · Jul 27, 2011 at 07:52 PM

Use this.

http://unity3d.com/support/documentation/ScriptReference/Vector3.RotateTowards.html

Or if you're into quaternions, you can use

http://unity3d.com/support/documentation/ScriptReference/Quaternion.RotateTowards.html

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anykey · Jul 27, 2011 at 08:33 PM 0
Share

Thanks for replying

var speedmin : float = 2; var speedmax : float = 4;

var from = new Vector3(0,0,0); var to = new Vector3(0,90,0);

Statue.transform.position = new Vector3.RotateTowards(from, to, speedmin, speedmax);

it still doesn't work though, I've got this error message: Assets/$$anonymous$$esAssets/$$anonymous$$esScripts/TriggerStatueRotation.js(32,41): BCE0017: The best overload for the method 'UnityEngine.Vector3.RotateTowards(UnityEngine.Vector3, UnityEngine.Vector3, float, float)' is not compatible with the argument list '(UnityEngine.Transform, UnityEngine.Transform, float, float)'.

avatar image almo · Jul 27, 2011 at 08:40 PM 0
Share

Read the error. It says you are trying to feed Transforms into a function expecting Vector3s.

The way RotateTowards works is you feed it two vectors: one that is where the object is pointing, and another which is where you want it to point. The result is a vector that has moved partway toward pointing the way you want it to when it's done.

avatar image Anykey · Jul 27, 2011 at 09:17 PM 0
Share

No more error , but the Statue GameObject is rotating to 3.9999 , not to 180 private var startPosition : Vector3;

var from : Vector3;

var to : Vector3;

var speedmin : float = 50;

var speedmax : float = 180;

function Start (){

 startPosition = transform.eulerAngles ;

}

from = startPosition;

to = Vector3(0,180,0);

Statue.transform.eulerAngles = Vector3.RotateTowards(from, to, speedmin, speedmax);

avatar image almo · Jul 27, 2011 at 09:19 PM 0
Share

You don't use RotateTowards with EulerAngles. Reread my comment. It takes a vector, and returns a vector that has been rotated to be closer to pointing toward the second vector.

avatar image Anykey · Jul 27, 2011 at 09:36 PM 0
Share

I'll try on tommorow ,

Statue.transform.eulerAngles = Vector3.RotateTowards(from, to, speedmin, speedmax);

I've tried rotation , localRotation , Rotate ins$$anonymous$$d of eulerAngles , but nothing work because they are all quaternions , I really don't know what to use

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotation Jumping values (0 to 180) 1 Answer

Object wont rotate in the opposite direction 1 Answer

My object isn't rotating to the transform of another object. How do I fix this? 1 Answer

Rotation on Android vs. PC 0 Answers

Rotating a 2D GameObject on Z regardless of the distance. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges