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
or i can't displayed the HighScore
– LyophilicRemove
– Dennettstatic
from this public static int score;