I have two pipes that are connected. I need some way to detect when they are rotating in opposite directions of each other and detach them (the direction they get unscrewed only).
The pipes are handled by machines and their local rotation don’t change. They are often linked to different objects, so parent is not too useful.
- I need some way to get the rotation of PipeA relative to PipeB and vica versa.
- If I use euler angles i need some way to deal with rotation popping from 360 degrees to 0.
I got a few methods working a little bit, but they are inconsistent and I need it to work 100% of the time.
PS: I don’t need the full range of unscrewing, I don’t care about the pipe threads or it needing to rotate a specific amount to get detached, just if angleDif > 5 degrees then detach().
Any help is greatly appreciated, thanks.
You shouldn’t think of “rotations” here but in terms of “orientation”. They are often used synonymously. However rotations actually represent the change of orientation (an action) while the orientation represents the state. Pipes usually have a “connection axis” which has to be aligned with the connection axis of it’s counterpart. The easiest solution is when the pipe is aligned with it’s local space axis. That way you can use transform.forward / .right, .up as the direction vector. If you have strange diagonal angles for your pipes you may want to add empty gameobjects at the connection points and orient those properly so transform.forward points along the connection axis.
When you connect two pipes you just have to make sure you align the two connection axis. If your pipes have a direction / opposite connectors (male, female) so they only fit together in one way you may want to use a directed axis. So the connector axis of the incoming connection points “inwards” and the connector axis of the outgoing connection points outwards. In this case the axis have to match exactly.
In any case using connector points (empty gameobjects) is the easiest and most flexible approach. In addition to the connection axis you get a whole coordinate space which also allows you to control the rotation around that axis (if that’s possible, round pipes?). To connect two pipes you actually connect the two connector objects of the two pipes. The easiest way is to temporarily swap the parent child oder of the pipe and it’s connector. So usually the connector is a child of the pipe. Now just unparent the connector and parent the pipe to the connector. They will preserve orientation and worldspace position. Now you can move and rotate the connector any way you like and the pipe will follow. To connect two pipes you just have to move the connector at the same position and orientation as the connector you want to connect to. If you want to allow any connector to connect to any other (no directed connector) you would need to flip the orientation since in this case the connectors all point outwards (or inwards) so two connected pipes will always have opposite connection axes.
Most rotation or orientation problems can be solved that way. With TransformDirection / TransformPoint and InverseTransformDirection / InverseTransformPoint you can easily get a certain direction vector or point from local space to worldspace or the other way round. When using connector objects you usually use those as reference.
As the question stands the actual setup is not really clear. I can update my answer with a more specific answer if the question is more specific. I’ve once created a trackmania like game for android where you can build your own track from various track pieces.