- Home /
changing transform of an object (without using "look at")
ok I have an object I want to align with the terrain surface... (lets say its an arrow pointing nowrth-west) I have the terrain surface normal... and if I assign this to the arrow's up component of its transform, it correctly hugs the surface... but... it doesnt face north west anymore... why ? arrow.transform.up = surfaceNormal;
why does changing the arrow's up affect its facign direction... it shouldn't unless when I change the UP... unity also resets the forward and right. I would expect it to just orthonormalize them.
does anyone have an explanation for this ? or a better wat to set the UP part of a transform, but still maintain the forward vector ? thx
Answer by JonnyHilly · Aug 09, 2010 at 06:53 AM
thanks muchly, that did the trick... here is the final code
//first calc forward vector, using surface normal, and player's right vector (to maintain facing direction)
Vector3 forwd = Vector3.Cross(playerRight ,terrainNormal );
//then calc the player's new rotation quat
transform.rotation = Quaternion.LookRotation(forwd, terrainNormal);
Answer by StephanK · Aug 05, 2010 at 09:16 AM
Setting the up/right/forward vector of a transform is never a good idea as this will almost always mess up at least one of the other 2 vectors. Althoug I agree with you that it would be nice if Unity would leave the other two axes untouched this isn't how it works.
What you will have to use is Quaternion.LookRotation(transform.forward, yourNormal).