- Home /
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 ?
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 }
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 ;)
Thanks for your offer - be sure I'll cry for help if I need!
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
Thanks for replying
var speed$$anonymous$$ : 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, speed$$anonymous$$, 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)'.
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.
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 speed$$anonymous$$ : 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, speed$$anonymous$$, speedmax);
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.
I'll try on tommorow ,
Statue.transform.eulerAngles = Vector3.RotateTowards(from, to, speed$$anonymous$$, 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