- Home /
Temporarily disabling rigidbody & configurable joint
Hi guys, I'm making a game where I need to be able to pick up and move a gameobject without it being able to interact with the environment. To do this I need to remove or disable its rigidbody, in order to stop it colliding with other objects and to disable its gravity. However, because I'm making a 2D game, I also need to remove the objects configurable joint component temporarily to do this? How would I go about doing so? I cant figure out the code at all.
After I've moved the object, I need to be able to replace it, which also means I need to re-enable the configurable joint and rigidbody with exactly the same settings before, to prevent it being moved in the z axis for my 2d game. How would I do this in Javascript.
Here's my code for moving the objects so far. As you can see, it's pretty simple, because I'm quite new at this. When the controller object this script is attached to moves in front of the object I want to move, the object to be moved becomes a child of the controller.
function Update () {
var fwd = transform.TransformDirection (Vector3.forward); //Defining the travel direction of the raycast vector var hit : RaycastHit; var LayerMask = 1 << 9; //Interacts with Layer 9 - Moveable
//if Raycast finds a collider in front of this object in layer 9, print message 1, else print message 2. if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) { print ("There is something in front of the object!");
var otherThing = hit.transform;
otherThing.rigidbody.useGravity = false; otherThing.parent = transform; }
else print ("Nothing in front of the object!");
}
So basically: 1.How can I disable rigidbodys and confiurable joints in code and 2. If I need to delete these components, whats the easiest way to recreate them in code?
Answer by Ashkan_gc · Mar 07, 2011 at 04:07 PM
generally there are two ways for disabling components. they either have enabled property (behaviours) and you can disable them or you need to remove them using Destroy() and add them again. for rigidbodies setting their isKinematic to false is same as disabling them.
If I make a rigidbody kinematic, will configurable joint also stop working temporarily?
Answer by dissidently · Mar 07, 2011 at 03:59 PM
A Joint becomes a [Component] of an object once it's connected to it.
You need to remove the joint component from the object, I think, from what you're saying.
However there's no Remove component. See here, last line of the page http://unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html (should be at the top of the page... but the Unity way is "most obvious and necessary things are taught to you last, Unity is deliberately obtuse about understanding the needs of our clients, we like watching you hunt for things. Given chances, we'll deliberately deceive and annoy. We have found ways to allow our customers to fill in where we are pretending to be too busy to assist, produce decent documentation or fully explain our feature set and capabilities, performance and deficiencies.")
Keep that above link, you're going to need it again in a moment.
So... removing the component "joint" is done with a destroy. As described on this page... http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html
But don't ask me how to EXACTLY syntactically express this. For that you're going to need trial and error, because again, that which is most obviously going to be of help to you, like EXACT syntax examples, is not going to be easy to find in the "Unity way".
Having removed you joint, you can freely do all your moving of stuff. then return to the AddComponent page that first told you there's no RemoveComponent.
For that, through trial and error, I know the exact syntax for adding a joint to an object once I have that object defined in a variable (again, something not obvious, but you'll need to figure out how to do from where ever the script is that you're trying to use to add the joint. First, set a variable to be the object that you're adding the joint to.
Then use that variable like this.
_var_holding_object_to_receive_new_joint.AddComponent(FixedJoint);
Good luck. And don't believe the hype. Unity documentation is not fantastic. It's HORRENDOUS. Especially considering who this engine is targeted at.
the documentation has some problems but hey that is not as bad as you said.
Answer by inz_przemyslaw_wozniak · Nov 03, 2014 at 09:11 PM
This C# code works fine:
Destroy(GetComponent<FixedJoint>());
Answer by miras · Jan 04, 2014 at 02:11 AM
void Update () { SpringJoint2D name = GetComponent(); name.enabled = false; }
Your answer
Follow this Question
Related Questions
Make isKinematic false when touching a Collider? 1 Answer
Temporarily removing rigidbody and configurable joint and then replacing them 0 Answers
Mesh renderer does not move with parent rigid body 5 Answers
Kinematic rigidbody only triggering once 0 Answers
enabling / disabling child objects 1 Answer