Fixed Joint - Collision Problem

Scenario: its like a temple run game, it has player(still), a platform(it moves) and obstacle(which will be created after a certain time until the game ends)

in my 3D game currently I have a

  1. moving rigidbody - platform(has velocity, platform like cube)
  2. another rigidbody - obstacle(no velocity, a cube)
  3. player - rigidbody (a cube)

At a time i have 2 platforms in the scene, they are destroyed and created to make a infinte platformer style game.

Obstacle are generated via script. the steps are -

  1. create a obstacle
  2. add a fixedJoint component to the obstacle
  3. set the connectedBody property of the fixedJoint to platform in the OnCollsionStay() function

so far, everything is working as expected.

problem occurs when the player collides with the obstacle, the collision happens abnormally. Like there was a explosion occurred and the player just shoots off from there. And if the player hits the top of the obstacle this abnormality is more clear.

I like to say that when i drag and drop the obstacle in the scene view, add joint, set connected body by hand instead of script the collision(between player and obstacle) is normal. but infinite obstacle cannot be generated by hand, I found out when i was testing these things.

I am putting the adding joint code and Creating obstacle code here.
First the create obstacle code —

#pragma strict

var obstacleSpawnPoint: GameObject;			// the position where the obstacle will be created
var obstacleHolderVariable: GameObject;		// obstacle prefab

function Start () {
	InvokeRepeating("CreateObstacle", 1, 3);
function CreateObstacle()
	//yield WaitForSeconds(5);
	Instantiate( obstacleHolderVariable, obstacleSpawnPoint.transform.position, obstacleSpawnPoint.transform.rotation );

Now the Joint adding code —

#pragma strict

var isObstacleCreated:boolean ;

function Start () {
	Destroy(gameObject, 15);
	isObstacleCreated = false;		// so that only one obstacle is created at a point at a time
function OnCollisionStay(collisionInfo : Collision) {
	if(isObstacleCreated == false)
		Debug.Log("entered - COLLISION___STAY ");
		var fixedJointVariable :FixedJoint =gameObject.AddComponent(FixedJoint);
		//var fixedJointVariable :FixedJoint =gameObject.GetComponent(FixedJoint);
		//var rigidbodyOfPlatform :Rigidbody = GameObject.Find("platformWhole_v2").rigidbody;

		// getting the rigidbody of that platform 
		var rigidbodyOfPlatform :Rigidbody = collisionInfo.gameObject.rigidbody;	
		if(rigidbodyOfPlatform == null || fixedJointVariable == null)
			Debug.Log("rigidbody NOT found!");
			// setting the connected body of fixedJoint(obstacle) to the platform rigidbody
			fixedJointVariable.connectedBody = rigidbodyOfPlatform;		
		isObstacleCreated = true;

The thing that bugging me most is i don’t even know how to figure out the problem, any suggestion will be appreciated

try setting anchor and connectedAnchor setting of fixedJoint with

fixedJointVariable.anchor = Vector3 (...); // refers to the the local position in respect to the player.
fixedJointVariable.connectedAnchor = Vector3(...); // refers to the local position relative to the obstacle.

If the above does not help, please try moving connectedBody line 27 and the above, just after creating the fixed joint. the problem might also be that, when you create a joint and do not attach it to a rigidbody, it attaches itself to world coordinates. Therefore, you might be first attaching your fixed joint to (0,0) right after you create the fixedjoint, causing the player to explode.

also a suggestion; why do you create the joint over and over? Just create one, when collision is detected, change connectedBody and enable, when collision is finished, disable and remove connectedbody. this might be more practical and fix your problem as well.