How do I make transform.Lookat use Vector3.down instead of Vector3.forward?

Hey guys,

This seems like a situation that should have come up before, but for some reason I am unable to find a simple solution…

I have a plane I am using for a wall, I have my bones all weighted and in position, I can manually move them into position…

Now why can’t I figure out how to make their y property point away from a Vector3?

say I have 2 bones, called bones, they are vertical at 0,0 and 0,1. they are 1 unit tall. They contain a weighted mesh between them. I want to be able to make them warp that mesh based on a point at (.5f,-10f,.5f) using the bones’ down vector.

I have been reading Quaternions and Eulers for 6 hours now /wrist. help! Here is a code smippet I have been playing with based on some tutorials…

(I am aware I should be looping and that my code is inefficient, it is torn down for debugging)

Vector3 DUMMYCENTER = new Vector3(.5f,-3f,.5f);
		Vector3[] Q2EAnglers = new Vector3[8];
		Q2EAnglers[0] = Quaternion.LookRotation(DUMMYCENTER - DSWBones[0].position).eulerAngles;
			//Q2EAnglers[0].x = 0;//
			Q2EAnglers[0].y = 0;//
			Q2EAnglers[0].z = 0;//
		Q2EAnglers[1] = Quaternion.LookRotation(DUMMYCENTER - DWWBones[0].position).eulerAngles;
			Q2EAnglers[1].x = 0;//
			Q2EAnglers[1].y = 0;//
			//Q2EAnglers[1].z = 0;//
		Q2EAnglers[2] = Quaternion.LookRotation(DUMMYCENTER - DEWBones[0].position).eulerAngles;
			Q2EAnglers[2].x = 0;
			Q2EAnglers[2].y = 0;
			//Q2EAnglers[2].z = 0;//
		Q2EAnglers[3] = Quaternion.LookRotation(DUMMYCENTER - DNWBones[0].position).eulerAngles;
			Q2EAnglers[3].x = 0;
			Q2EAnglers[3].y = 0;
			//Q2EAnglers[3].z = 0;//
		Q2EAnglers[4] = Quaternion.LookRotation(DUMMYCENTER - DSWBones[1].position).eulerAngles;
			//Q2EAnglers[4].x = 0;//
			Q2EAnglers[4].y = 0;//
			Q2EAnglers[4].z = 0;//
		Q2EAnglers[5] = Quaternion.LookRotation(DUMMYCENTER - DWWBones[1].position).eulerAngles;
			Q2EAnglers[5].x = 0;
			Q2EAnglers[5].y = 0;
			//Q2EAnglers[5].z = 0;//
		Q2EAnglers[6] = Quaternion.LookRotation(DUMMYCENTER - DEWBones[1].position).eulerAngles;
			Q2EAnglers[6].x = 0;
			Q2EAnglers[6].y = 0;
			//Q2EAnglers[6].z = 0;//
		Q2EAnglers[7] = Quaternion.LookRotation(DUMMYCENTER - DNWBones[1].position).eulerAngles;
			Q2EAnglers[7].x = 0;
			//Q2EAnglers[7].y = 0;
			Q2EAnglers[7].z = 0;//
		
		
		DSWBones[0].rotation = Quaternion.Euler(Q2EAnglers[0]);
		DWWBones[0].rotation = Quaternion.Euler(Q2EAnglers[1]);
		DEWBones[0].rotation = Quaternion.Euler(Q2EAnglers[2]);
		DNWBones[0].rotation = Quaternion.Euler(Q2EAnglers[3]);
		DSWBones[1].rotation = Quaternion.Euler(Q2EAnglers[4]);
		DWWBones[1].rotation = Quaternion.Euler(Q2EAnglers[5]);
		DEWBones[1].rotation = Quaternion.Euler(Q2EAnglers[6]);
		DNWBones[1].rotation = Quaternion.Euler(Q2EAnglers[7]);

Maybe there is a way to extend Transform? Maybe overload the Lookat() Method?

cmon, there has to be a solution for this...

1 Answer

1

Create a standard rotation that moves those vectors in general and then multiply the LookRotation by that:

var rotForwardToDown = Quaternion.FromToRotation(Vector3.forward, - Vector3.up);

 var rotation = (Quaternion.LookRotation(DUMMYCENTER - DEWBones[1].position) * rotForwardToDown).eulerAngles;

Fantastic! Someone needs to hunt down the creator of Quaternions and dumb him down a bit... preferably with a frying pan. I was reading your tutorial over at http://unitygems.com/quaternions-rotations-part-1-c/ about this... but when I tried it I had issues. I need to figure out how to Clamp quaternions now and I think I will be golden, getting some weird rotations now. Thanks again WhydoIdoit, huge help!

I answer a question here http://answers.unity3d.com/questions/420881/limiting-rotation-of-object-specifically-using-scr.html with a function to clamp angles

Yeah Quaternions are definitely mind bending. You have to really forget about eulerAngles and think about what you would want to do with a vector... Where do you want it to point.

Ok, so I'm sure you remember the Spherized cube question I asked a while back. I pass quads on that sphere to a method and Instantiate a floor/walls on the grid, and they are all facing outward now, however their local rotations on the x/y axis need to remain uneffected by this solution. The localrotation property of the transform is a quaternion though, so I can't just apply a Vector3 to it... Or maybe I can apply that method above for combining quaternions... hrm

BTW, @Cyber_Defect ... the creator of quaternions (http://en.wikipedia.org/wiki/William_Rowan_Hamilton) was dumbed down in a most final way in 1865. I don't believe any frying pans were involved.