UnityEngine.Quaternion vs UnityEngine.Transform : Values

Hi there,
I am just messing around trying to set up a spot light which will rotate from and to 5 different rotations looping back to 1 when it reaches 5. Anyhow, I am using this code…

var rot1 : Transform;
var rot2 : Transform;
var rot3 : Transform;
var rot4 : Transform;
var rot5 : Transform;
var speed : float;
function Update () {
	if(rot1 == transform.Transform)
	{
		print("at rot1 pos");
		transform.rotation = Quaternion.Slerp (rot1, rot2, Time.time * speed);
	}
	if(rot2 == transform.Transform)
	{
		print("at rot2 pos");
		transform.rotation = Quaternion.Slerp (rot2, rot3, Time.time * speed);
	}
	if(rot3 == transform.Transform)
	{
		print("at rot3 pos");
		transform.rotation = Quaternion.Slerp (rot3, rot4, Time.time * speed);
	}
	if(rot4 == transform.Transform)
	{
		print("at rot4 pos");
		transform.rotation = Quaternion.Slerp (rot4, rot5, Time.time * speed);
	}
	if(rot5 == transform.Transform)
	{
		print("at rot5 pos");
		transform.rotation = Quaternion.Slerp (rot5, rot1, speed);
	}					
}

…however I am getting the error…

Obviously it means that the values I am plugging in are in valid, correct? Anyhow, going on what the error says, only quaternion values are excepted into the method. However, quaternion are complex numbers and I thought that plugging in transform.rotation values would be appropriate. What am I doing wrong?

transform.rotation = Quaternion.Slerp (rot1, rot2, Time.time * speed);

Yes, rot1 and rot2 need to be defined as Quaternions and not Transforms.

And yes, quaterion is more complex and it need more time to be learnt.

I’m starting learning it :stuck_out_tongue:

you can define rot1 as Quaternion and then asing a rotation using a transform.rotation because it is a quaterion too.

Anyways i can’t understand your script, sorry.

I dont get it tho.

I just passed the var as a Quaternion and I get more errors. First of all passing it as such did seem weird, but… I thought a transform.rotation is a quaternion so if you declare a ver as a transform, it can translate no?

Also, how should I declare my var for rotations then?

ok, so I changed the code, but still get an error.

var transform : Transform; 
var rot1 : Quaternion;
var rot2 : Quaternion;
var rot3 : Quaternion;
var rot4 : Quaternion;
var rot5 : Quaternion;
var speed : float;
function Update () {
	var crntRot : Quaternion = transform.rotation;
	
	if(rot1 == crntRot)
	{
		print("at rot1 pos");
		transform.rotation = Quaternion.Slerp (rot1, rot2, Time.time * speed);
	}
	if(rot2 == crntRot)
	{
		print("at rot2 pos");
		transform.rotation = Quaternion.Slerp (rot2, rot3, Time.time * speed);
	}
	if(rot3 == crntRot)
	{
		print("at rot3 pos");
		transform.rotation = Quaternion.Slerp (rot3, rot4, Time.time * speed);
	}
	if(rot4 == crntRot)
	{
		print("at rot4 pos");
		transform.rotation = Quaternion.Slerp (rot4, rot5, Time.time * speed);
	}
	if(rot5 == crntRot)
	{
		print("at rot5 pos");
		transform.rotation = Quaternion.Slerp (rot5, rot1, speed);
	}					
}

This error occurs on all lines in the function which have transform in them.

And it is quite right. “transform” is ambiguous between the public variable you declared named “transform” and the MonoBehaviour property named “transform”. I would suggest you make it less ambiguous by renaming your variable.

Ok thanks, that did clear up the ambiguous. But I thought

var transform : Transform;

was common and that then using transform throughout your script would refer to the owner of that script. No??

///
Anyhow, beyond that I am still having issues with this, perhaps some one can help me out…

var ownerTransform : Transform; 
///each "rot#" val is a rotation to be plugged in
var rot1 : Quaternion;
var rot2 : Quaternion;
var rot3 : Quaternion;
//crntRot will be used in update to measure the currnet rotation
var crntRot : Quaternion;
///speed at whcih owner will rotate
var speed : float;
function Update () {
	
	///retrieve currnet rotation value of object.
	crntRot = ownerTransform.transform.rotation;
	print("ownerTransform.rotation " + ownerTransform.rotation);
	print("rot1 " + rot1);
	print("rot2 " + rot2);
	print("rot3 " + rot3);
	print("crntRot " + crntRot);
	///compare
	if(rot1 == crntRot)
	{
		print("at rot1 pos");
		ownerTransform.transform.rotation = Quaternion.Slerp (rot1, rot2, Time.time * speed);
		return;
	}
	
	///compare	
	if(rot2 == crntRot)
	{
		print("at rot2 pos");
		ownerTransform.transform.rotation = Quaternion.Slerp (rot2, rot3, Time.time * speed);
		return;
	}
	
	///compare
	if(rot3 == crntRot)
	{
		print("at rot3 pos");
		ownerTransform.transform.rotation = Quaternion.Slerp (rot3, rot1, Time.time * speed);
		return;
	}
					
}					
}

The problem I am having is this…
the prints return …
a. the owner, does not rotate.
b. are the quarternion values always no greater than 1? If so, plugging in desired rotation values as I have, is this incorrect?
c. w in the quaternion can be set, but should “not be messed with”, what value should it hold?

ownerTransform.transform.rotation = Quaternion.Slerp (rot1, rot2, Time.time * speed);
?¿?¿
a transform can’t have another .transform. …

and…

tip: you don’t need any ownerTransform because if this file.js is in one Object, then… you can use transform.rotation, transform.Translate, transfrom. all you want, and it refers your Object which has the script.

thats what i thot???
and i got the ambiguous!

anyhow, no worries, more testing after lunch.

:::)))(((

Ok,
My first question…

var ownerTransform : Transform; 
///each "rot#" val is a rotation to be plugged in
var rot1 : Quaternion;
var rot2 : Quaternion;
var rot3 : Quaternion;
///speed at whcih owner will rotate
var speed : float;
function Update () {
	///retrieve currnet rotation value of object.
	var crntRot = ownerTransform.rotation;
	print("ownerTransform.rotation " + ownerTransform.rotation);
	print("rot1 " + rot1);
	print("rot2 " + rot2);
	print("rot3 " + rot3);
	print("crntRot " + crntRot);
	///compare
	if(rot1 == crntRot)
	{
		print("at rot1 pos");
		ownerTransform.rotation = Quaternion.Slerp (rot1, rot2, Time.time * speed);
		return;
	}
	
	///compare	
	if(rot2 == crntRot)
	{
		print("at rot2 pos");
		ownerTransform.rotation = Quaternion.Slerp (rot2, rot3, Time.time * speed);
		return;
	}
	
	///compare
	if(rot3 == crntRot)
	{
		print("at rot3 pos");
		ownerTransform.rotation = Quaternion.Slerp (rot3, rot1, Time.time * speed);
		return;
	}
					
}

From this, can you see or tell me what I am doing wrong? I ask because “crntRot” always returns a value not equal to rot1 but the print also implies I am in the if(rot1 == crntRot).

:?

You know, you might be best off trying out iTween instead. I’m pretty sure a series of iTween.RotateTo functions would do exactly what you’re looking for.

ok, so my problem is less language, defining etc then method.

ok, thanks.

it should be Time.deltaTime

also, I assume you are assigning values into speed, rot1, rot2,… somewhere

Thanks. iTween is a paid plug in right? Not necessary from my point as this code is me just trying to learn.

Yes, I did assign values to the rot# val’s. Interesting this is…

var crntRot = transform.rotation;
print(crntRot);

anyone value never exceeds 1.0
also, the camera seems to just stall, shaking and vibrating in one virtual (as it moves a little bit) direction/rotation. Very weird. Again this code is not too important I just wanted to mess around a bit.

Thanks.

did you try it with Time.deltaTime?

alternatively as a test, remove the speed stuff and replace with 0.1f

yes.

var ownerTransform : Transform; 
///each "rot#" val is a rotation to be plugged in
var rot1 : Quaternion;
var rot2 : Quaternion;
var rot3 : Quaternion;
///speed at whcih owner will rotate
var speed : float;
function Update () {
	///retrieve currnet rotation value of object.
	var crntRot = ownerTransform.rotation;
	print("rot1 " + rot1);
	print("rot2 " + rot2);
	print("rot3 " + rot3);
	print("crntRot " + crntRot);
	///compare
	if(rot1 == crntRot)
	{
		print("at rot1 pos");
		ownerTransform.rotation = Quaternion.Slerp (rot1, rot2, Time.deltaTime * 0.1f);
		return;
	}
	
	///compare	
	if(rot2 == crntRot)
	{
		print("at rot2 pos");
		ownerTransform.rotation = Quaternion.Slerp (rot2, rot3, Time.deltaTime * 0.1f);
		return;
	}
	
	///compare
	if(rot3 == crntRot)
	{
		print("at rot3 pos");
		ownerTransform.rotation = Quaternion.Slerp (rot3, rot1, Time.deltaTime * 0.1f);
		return;
	}				
}

one thing. the var ownerTransform asks for an object in the inspector. Ideally I want this to be the script owner. So naming it as such is not an issue correct? It just seems odd, defining the owner as the ownerTransform. Regardless. that is the code I am working with. The initial rotation of the object is == to rot1 value I plugged in, so that should get the ball rolling as they say, but it just stays still. Still.

:?

You don’t need to define the ownerTransform at all. The script knows who its owner is.

transform.rotation = slerp(…)

also, you have taken my suggestion and gotten it backwards… Time.deltaTime is usually in the vicinity of 0.01 (very small), 0.01 * 0.1 = very small value (ie hardly rotate at all).

try Time.deltaTime * 10 or 0.1f

transform.rotation = Quaternion.Slerp (rot1, rot2, Time.deltaTime * 10);

or

transform.rotation = Quaternion.Slerp (rot1, rot2, 0.1f);

You need to keep in mind that Update() is called as fast as it can, which is likely to be up around 100 times/second, if you specify a rotation float of over 1, you are going to see some really fast rotation, so fast it might be doing several full spins in one update.

ok, so here are my readings at what is happening.
At start…
readings…
rot1 (0.0, 0.0, 0.0, 1); //// this is fine, set in the inspector, matching…
crntRot (the owners rotation).

Result with 0.1f, is an instant switch from rot1 to rot 2, a near 180 with out any of the desired effect I had wanted of seeing the light rotate at human speed.

then, at rot 2
the console spits out that the crntRot is (-0.7, -0.6, 0.4, 0.0);
and it sits there, at that rotation infinitely.

Scratching head.

try

transform.rotation = Quaternion.Slerp (transform.rotation, rot2, 0.1f);

mm.
no.
it just rotates it (rotates it meaning, instantly changes rotation angle) to one random angle. then nothing.