# Making my pipes face the correct direction

I am trying to dynamically generate a pipping system. I am using a very specific way to connect the pipes after generating them. The forward has to be pointing at the next pipe and the right has to be pointing at the previous pipe.

Is it possible to do something like

transform.right = prevPipe.transform.position - transform.position;
transform.forward = nextPipe.transform.position - transform.position;


Take effect at the same time instead of being 2 individual amendments?

One would solve these kinds of rotation with Quaternions.

Unity has a quaternion function called SetLookRotation that allows you to specify the forward and up vectors at the same time.

Since in your case you only have forward and right, you can calculate the desired up vector by crossing the vectors of the two pipes.

Vector3 up = Vector3.Cross(nextPipe.transform.position - transform.position,
prevPipe.transform.position - transform.position);

Quaternion newRot = new Quaternion();
newRot.SetLookRotation(nextPipe.transform.position - transform.position, up);

transform.rotation = newRot;


Hope that helps, Yong Woon :)