Instantiate and rotation between 2 vector3 points

Hi,

I am trying to instantiate cube prefab between 2 objects.I am drawing lines with vectorsity and when i completed shape my script trying to instantiate object between 2 points.alt text

My lines using vector3 and also i am working on X / Y axis. when i instantiate prefab between 2 points i have to rotate them in Z axis. But when i tried to instantiate an object there is no rotations applied.

That black line is my cube on isometric cam view and i want to rotate them between 2 vector3 points. I need some advices for calculating rotation on z axis.

How can i do that ?

Sorry for my bad english

Best Regards and Thanks

To align the object between two points you can compute the z rotation using this code snippet:

Vector3 p1, p2;
float zRot = Vector3.Angle( Vector3.up, p2 - p1 );

The up vector is our reference (aim) vector.

Hi,

Thanks but still not working.
I am calculating center. And trying to instantiate and rotate .

Vector3 nPosition = Vector3.Lerp (linePoints[i] , linePoints[i+1], 0.5f);
			float zRot = Vector3.Angle( Vector3.up, linePoints[i+1] - linePoints[1] );


			wallObjects[i] = (GameObject)Instantiate(Resources.Load("WallCube"), nPosition, Quaternion.identity);


			wallObjects[i].transform.rotation = new Quaternion(wallObjects[i].transform.rotation.x,wallObjects[i].transform.rotation.y,zRot,wallObjects[i].transform.rotation.w);

Sorry, the code segment did not handle negative angles.

Vector3 p1, p2;
float zRot = Vector3.Angle( Vector3.up, p2 - p1 );
if( p2.x - p1.x < 0.0f )
 zRot *= -1.0f;

To build the quaternion you need a rotation axis (xyz) and the rotation angle (w):

Quaternion q = new Quaternion( 0.0f, 0.0f, 1.0f, zRot ); // rotate about z axis

I am not sure if all these operations use degree or radians, you would have to convert the value appropriately.

PS: Unity also has “look at” functions like Quaternion.SetLookRotation( … ). It may be easier to use.

I think you can use lookRotation:

yes it works but i just want rotate in z axis. it rotates in every axis . i just want instantiate prefab and change z rotation value of object.

Use imaginary points that are aligned on the other two axis.

Hi,
I’m just creating lines between Levelbuttons :slight_smile:
This works for me :
Create image, stretch it to correct height, apply z-rotation, move to center of the 2 objects.

3511537--280435--Capture1.PNG