Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 subhash147 · Jun 27, 2016 at 03:41 PM · unity 5rigidbody2dgravityoncollisionenter

how to disable gravity on collision of rigidbody2d?

I want to disable gravity on collision of two rigidbody2d .

First of all i dont want default collision effect by unity so i have checkd istrigger,so do i need to check istrigger for both rigdid bodies?

do i have to use istrigger function or enterooncolllider fucntion.?

can you tell me the exact syntax for disabling gravity?

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
0

Answer by Dibbie · Jun 27, 2016 at 05:03 PM

Rigidbodies (2D or 3D) all have gravity, the difference for 2D is, its a value instead of a boolean, and since your using a trigger, youd have to use the command OnTriggerEnter2D, and OnTriggerStay2D may also be of some help to you.

To setup a proper gravity disabled function, it may look something like this:

 void OnTriggerEnter2D (Collider coll){
 coll.gameObject.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the collided object
 this.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the objected collided into (IE, this one)
 }

If you have other things in your world with rigidbodies and some type of colliders, you may want to create a tag for each object you want to collide into, something like "enemy", or access the objects name directly if you know the exact name of the objects you want to collide with, keeping in mind, Unity will automatically add a bracketed number to multiple objects of the same instance, and add "(Clone)" to prefabbed objects created into the game world. So if you have multiple prefabs in the world, you may see "Enemy (Clone) (1)" and "Enemy (Clone) (2)" and so on...

To set up a tag, select any object from your hierarchy, and go into your Inspector tab, at the top left of that, you will see (most likely) "Tag: Unassigned", click on the "Unassigned" to bring up a drop-down, by default theres about 5 in there, but the last one youll see as "Create new..." or something similar - clicking on that will convert your Inspector into the Tag editor, so you can add custom tags.

After one is created, click on the object you want to have that tag on, go to the Inspector for that object, and where it says "Tag" at the top left, clicking on "Unassigned" to bring the dropdown back up, you will now see your tag there, click it to assign it to that object.

In code, you can now reference that tag in your trigger collision.

  void OnTriggerEnter2D (Collider coll){
 if(coll.gameObject.tag == "Enemy"){ //You would change "Enemy" to the exact name of whatever you called your tag - case sensitive
     coll.gameObject.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the collided object
     this.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the objected collided into (IE, this one)
     }
 }

Or by the exact object name:

  void OnTriggerEnter2D (Collider coll){
 if(coll.gameObject.name == "Enemy"){
     coll.gameObject.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the collided object
     this.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the objected collided into (IE, this one)
     }
 }

Or even by Contains, in case its a spawned prefab and you may not always have the exact name

  void OnTriggerEnter2D (Collider coll){
 if(coll.gameObject.name.contains("Enemy")){
     coll.gameObject.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the collided object
     this.GetComponent<Rigidbody2D>().gravity = 0f; //disable gravity on the objected collided into (IE, this one)
     }
 }

^ so if that object has the word "Enemy" in its name, itll be detected, if that is what so happened to have collided with eachother

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 subhash147 · Jun 29, 2016 at 04:44 PM

suppose my gameobject name is ball. So can i just not refer to it by ball.gravity scale? or do i have to specificly use the following: coll.gameObject.GetComponent().gravity = 0f;

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 Dibbie · Jun 30, 2016 at 11:19 PM 0
Share

@subhash147 - "coll" would be for the object that collided with whatever object your script is attached to, so if your script is attached to a ball, and the ball hits a wall, then "coll" is that wall.

Either way using ball or coll, you would have to use GetComponent() in that kind of style, or it may throw an error at you. What is in the is the name of the component that "gravity" would exist on - being physics.

So:

ball.GetComponent().gravity = 0f;

or

coll.gameObject.GetComponent().gravity = 0f;

avatar image
0

Answer by pradip_spixel · Jul 23, 2018 at 04:17 AM

is there any way to reduce speed of collided objects??? in my case two object are collided and speed of both are increased

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 unity_l6oP-q3Ct-J0QQ · Sep 27, 2021 at 11:00 AM

The below code can disable the gravityscale of individual rigibody2D :

public Rigidbody2D rb;

void OnTriggerEnter2D (Collider2D hitinfo) { rb.velocity = Vector2.zero; rb.gravityScale = 0f; Debug.Log(hitinfo.name); }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Upgraded to Unity 5, Animators are causing issues. 1 Answer

Gravity not working in a sidescrolling 2d game after upgrade to Unity 5 2 Answers

Make object use gravity without rigidbody. 2 Answers

Gravity Change + Flip 1 Answer

Usegravity for unity2d? 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