- Home /
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);
//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
I think you have a problem of anchor and connected anchor points. The joint might have been trying to connect to somewhere else.
can you please explain it a little better? i don't really know about anchor and how it effects other things :) This abnormal problem is driving me crazy! :( :(
Hey! there is no anchor point in Fixed Joint, anchors are in Hinge Joint.
Fixedjoint also has anchor and connectedanchor values, even if not in the editor.
Answer by koray1396 · Apr 02, 2014 at 05:13 PM
https://docs.unity3d.com/Documentation/ScriptReference/FixedJoint.html
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.
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) 1. so i have to create obstacles repeatedly and add joint component, and then set its connected body. 2. I have set its anchor to Vector(0, 0, 0). It still does not work :( 3. Joint.autoConfigureConnectedAnchor is set to True. so it is set automatically. Should i change it? 4. I have change code line 27 as you suggested
function OnCollisionStay(collisionInfo : Collision) {
if(isObstacleCreated == false )
{
var rigidbodyOfPlatform :Rigidbody = collisionInfo.gameObject.rigidbody;
var fixedJointVariable :FixedJoint =gameObject.AddComponent(FixedJoint);
fixedJointVariable.anchor = Vector3(0, 0, 0);
fixedJointVariable.connectedBody = rigidbodyOfPlatform;
isObstacleCreated = true;
}
}
what can i do figure out what the error is :( :(
Your answer
Follow this Question
Related Questions
How do I connect two objects together in the scene? 1 Answer
Animator overriding collisions? 0 Answers
Why does my player fly through my barrier? 2 Answers
Colliding fast moving object with a slow moving object 1 Answer
Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer