Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Tryptex · Oct 08, 2017 at 05:19 PM · unity 5colliderrigidbody2ddisableenable

How to change settings to a rigidbody when adding it to a gamobject?

I am trying to make it so that when a particular gameobject touches a game object with this script attached to it, it will add a rigidboy to the object causing it to fall and disable the collider so it can pass through objects, and after a predesignated amount of time cause it to reappear when it originally was turn destroy it's rigidbody and turn on it's collider, I got it all to work perfectly except for one tiny detail, I need to freeze the rigidbody's rotation on the z axis, how to I do this?

HERE IS MY CODE:

public float respawnDelay; public float WaitTime;

 public GameObject ground;

 public Rigidbody2D rb;

 public Collider2D goingThroughObjects;

 private Vector3 whereToTeleport;


 // Use this for initialization
 void Start ()
 {
     goingThroughObjects = GetComponent<Collider2D>();

     rb.isKinematic = true;
     goingThroughObjects.enabled = true;
 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if(other.tag == "HeadStomp")
     {
         StartCoroutine("TouchedBreakableObjectCo");
     }

 }

 public IEnumerator TouchedBreakableObjectCo()
 {
     whereToTeleport = ground.transform.position;
     yield return new WaitForSeconds(WaitTime);
     ground.AddComponent<Rigidbody2D>();
     rb = GetComponent<Rigidbody2D>();
     yield return new WaitForSeconds(0.2f);
     goingThroughObjects.enabled = false;
     yield return new WaitForSeconds(respawnDelay);
     Destroy (rb);
     goingThroughObjects.enabled = true;
     ground.transform.position = whereToTeleport;


PLEASE HELP

people I wanted to come help me:

@ UnityCoach

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

2 Replies

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

Answer by Hellium · Oct 09, 2017 at 03:37 PM

As indicated by tormentoarmagedoom, you can do the following :

 // ...
 rb = GetComponent<Rigidbody2D>();
 rb.constraints = RigidbodyConstraints2D.FreezeRotation ;
 // ...

Be careful, Rigidbody2D.constraints uses bitwise operations. For example, if you want to prevent the rotation on all the axes, and the translation on the X axis, use :

 rb.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation ;
Comment
Add comment · Show 4 · 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 Tryptex · Oct 09, 2017 at 03:42 PM 0
Share

ok, I didn't know how to do that before but there is one problem with the code, I am working with a Rigidbody2D, not just a Rigidbody, and now visual studio is co$$anonymous$$g back saying that it cannot convert a normal Rigidbody, into a Rigidbody2D, is there a way to change it to work with a Rigidbody2D?

avatar image Tryptex · Oct 09, 2017 at 03:44 PM 0
Share

@Hellium oops, forgot to add the at symbol and input your name.

avatar image Hellium Tryptex · Oct 09, 2017 at 03:56 PM 1
Share

$$anonymous$$y bad, I edited my answer to give you the 2D version of Rigidbody constraints.

avatar image Tryptex Hellium · Oct 09, 2017 at 04:00 PM 0
Share

@Hellium Ok, Thank you sooo much, I have been stuck on that for the past three months and haven't made any progress until now, thanks

avatar image
0

Answer by tormentoarmagedoom · Oct 09, 2017 at 07:58 AM

Hello !!

You have Rigidbody.freezeRotation and Rigidbody.constraints.

Comment
Add comment · Show 2 · 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 Tryptex · Oct 09, 2017 at 03:31 PM 0
Share

What do you mean by that? are you suggesting that I make a line of code that says: rb.freezeRotation.z; if so, please tell me, if not please tell me what you mean by that.

avatar image Tryptex · Oct 09, 2017 at 03:45 PM 0
Share

@tormentoarmagedoom oops sorry, forgot to add the at symbol and your name.

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

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Box and circle Collider doesn't work 1 Answer

Disable Collider After two Second of Being Triggered and enable it again 3 Answers

How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers

Why does Unity disable the Collider when I Destroy the Rigidbody? (2D) 1 Answer

Move a Rigidbody2d After Collision 0 Answers


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