How to disable gravity on collision of rigidbody2d?
Asked Answered
Q

4

0

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?

Quenchless answered 24/1 at 19:17 Comment(2)

or i can't displayed the HighScore

Lyophilic

Remove static from this public static int score;

Dennett
P
0

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 each other

Postiche answered 24/1 at 19:20 Comment(0)
Q
0

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;
Quenchless answered 24/1 at 19:21 Comment(1)

@Quenchless - "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;

Postiche
H
0

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

Higgins answered 23/7, 2018 at 4:17 Comment(0)
S
0

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);
}
Smoulder answered 24/1 at 19:21 Comment(1)

I am happy that everything is working for you now:)

Dennett

© 2022 - 2024 — McMap. All rights reserved.