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
- moving rigidbody - platform(has velocity, platform like cube)
- another rigidbody - obstacle(no velocity, a cube)
- 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 -
- create a obstacle
- add a fixedJoint component to the obstacle
- 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);
//CreateObstacle();
}
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!");
}
else
{
// 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