At the game start, I check the four directions from the object, and if there’s another object within range, they should form joints with each other. However, when an object has more than one object within range, it only joints with one of them. It does create the correct number of joints, but only one has it’s target game object assigned to it.
The script for each direction is essentially the same, and in my testing it didn’t only fail in one direction, so I posted only one directions script here.
Though I do wonder if this has something more to do with general unity behavior?
[SerializeField]
private Transform eastPosition;
[SerializeField]
private Transform westPosition;
[SerializeField]
private Transform northPosition;
[SerializeField]
private Transform southPosition;
float baseCastDistance = 0.2f;
private void Start()
{
CastEast();
CastNorth();
CastWest();
CastSouth();
}
void CastEast()
{
float castDistance = baseCastDistance;
Vector3 targetPosition = eastPosition.position;
targetPosition.x += castDistance;
Debug.DrawLine(eastPosition.position, targetPosition, Color.cyan, 1000f);
RaycastHit2D hit = (Physics2D.Linecast(eastPosition.position, targetPosition, 1 << LayerMask.NameToLayer("Blocks")));
if (hit)
{
print("Item in the way: " + hit.rigidbody.gameObject.name);
gameObject.AddComponent<FixedJoint2D>();
gameObject.GetComponent<FixedJoint2D>().connectedBody = hit.rigidbody;
}
}