- Home /
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?
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
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;
@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;
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
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); }
Your answer
