I’m working on my first Unity project, and it is a simple pipe-based puzzle game. The idea is that you pick up sections of pipe to build a pipeline. If you have a curved piece, the player can rotate it to make the pipe go left, right, up, or down. To accomplish this, I have two child objects of my pipe that act as simple colliders - one for each end.
This snapping and rotation is the part I’m having trouble with. If the two object’s natural origin aligns, then snapping works fine. If the origins do NOT align, however, things go crazy and the pipe will snap backwards, or inside the previous pipe, or sometimes even vertically.
Here is the code I’m using for snapping:
void OnTriggerEnter(Collider other)
{
// NOTE: rb is the transform.parent's RigidBody component, set in Start() (not shown)
rb.isKinematic = true;
// This is doesn't work.
//parent.transform.position = other.transform.parent.position;
//parent.transform.rotation = other.transform.parent.rotation;
// Figure out our rotation offset.
// TODO: What if we want to rotate our pipe UP/LEFT/RIGHT/DOWN?
Vector3 rotvector = other.transform.eulerAngles;
// I think I need to do something with these, but can't figure it out
//float direction = Mathf.Sign(transform.forward.z);
//float pitch = Mathf.Sign(transform.forward.y);
//float spin = Mathf.Sign(transform.forward.x);
//Debug.Log("Direction: " + direction + " | Spin: " + spin);
//Debug.Log("Forward: " + transform.forward);
//Quaternion rotoffset = Quaternion.Euler(rotvector.x * direction, rotvector.y * pitch, rotvector.z * spin);
Quaternion rotoffset = Quaternion.Euler(rotvector.x, rotvector.y, rotvector.z);
parent.transform.rotation = rotoffset;
// To figure out how much we need to move ourselves, we calculate the offset between our two snap points!
// Always do this *after* our rotation, because otherwise our endpoints may move!
Vector3 offset = transform.position - other.transform.position;
parent.transform.position -= offset;
}
I’ve tried several different combinations of using forward vectors and other things, but I just can’t seem to figure this one out.
If you connect them AND their natural origins happen to line up, everything is fine:
In other cases, the snapping goes bad:
Can anyone point me in the right direction as to how I am handling this wrong?