Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Alienjesus · Mar 07, 2011 at 06:56 AM · gameobjectrigidbodyenablekinematicconfigurablejoint

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alienjesus · Mar 07, 2011 at 07:32 PM 0
Share

If I make a rigidbody kinematic, will configurable joint also stop working temporarily?

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ashkan_gc · Mar 07, 2011 at 04:06 PM 0
Share

the documentation has some problems but hey that is not as bad as you said.

avatar image
1

Answer by inz_przemyslaw_wozniak · Nov 03, 2014 at 09:11 PM

This C# code works fine:

 Destroy(GetComponent<FixedJoint>());
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by miras · Jan 04, 2014 at 02:11 AM

void Update () { SpringJoint2D name = GetComponent(); name.enabled = false; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges