0
\$\begingroup\$

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?

Make the Forward point at the next Pipe And Make the Right point at the previous pipe End Result

\$\endgroup\$
4
\$\begingroup\$

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 :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.