x


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 ?

more ▼

asked Jul 27 '11 at 07:49 PM

Anykey gravatar image

Anykey
16 10 12 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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
}
more ▼

answered Jul 29 '11 at 01:35 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 42 202

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

Jul 29 '11 at 03:04 PM Anykey

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

Jul 29 '11 at 05:05 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 27 '11 at 07:52 PM

almo gravatar image

almo
1.8k 3 6 18

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/MesAssets/MesScripts/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)'.

Jul 27 '11 at 08:33 PM Anykey

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.

Jul 27 '11 at 08:40 PM almo

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);

Jul 27 '11 at 09:17 PM Anykey

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.

Jul 27 '11 at 09:19 PM almo

I'll try on tommorow ,

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

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

Jul 27 '11 at 09:36 PM Anykey
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2244
x1330
x747
x142
x18

asked: Jul 27 '11 at 07:49 PM

Seen: 1721 times

Last Updated: Jul 29 '11 at 05:05 PM