- Home /
Best way to manually calculate collision between GameObjects and a Tilemap without using physics
I'm currently trying to setup a 2D project that won't use any physics in GameObject movement (think retro top-down game movement calculation). Right now I'm trying to setup the player to collide with certain tiles on a Tilemap, but I'm running into issues with trying to determine where the player is colliding with the Tilemap.
I've currently setup the player with: A BoxCollider2D with isTrigger set to true because triggers are apparently ignored by the physics engine, which I'm not wanting to be used. A RigidBody2D with body type of Kinematic because I don't want the player to be affected by gravity, forces, etc.
The Tilemap has a TilemapCollider2D attached to it.
I was attempting to check for collision in a script attached to the player similar to the following:
private void OnTriggerEnter2D(Collider2D collision) {
...
ContactPoint2D[] contacts = new ContactPoint2D[1];
int numContacts = collision.GetContacts(contacts);
...
However this doesn't work because according to the API: "Contacts involving a Collider2D set to be a trigger will never be returned here because trigger Colliders do not have contact points."
So, at this point my question is: how exactly should I be setting stuff up and checking for collisions if I don't want any physics to be utilized?
Answer by yourecrippled · Dec 30, 2019 at 07:29 AM
It's always better and more accurate to use physics . And , it's supposed to work in the way you did . But , be sure that you have rigidbody and collider together on both sides - tile and gameobject itself- (doesn't matter if its kinematic or not) . Because, triggers are not ignored by the physics engine that' would be odd :) .
But still you can try to calculate the distance between two objects (If objects are sphere,circle etc. ) . For example ;
public float Radius = 10f;
private void Update()
{
float distance = (transform.position-targetTransform.position).magnitude;
if(distance<Radius){ Debug.Log("Hit"); }
}
Your answer
Follow this Question
Related Questions
How to move objects away from each other (not what you think) 1 Answer
Collision stops working at certain situations. Help? 1 Answer
Make 2D projectile collide only from the outside 1 Answer
Collision Killing CPU/FPS/Performance - Suggestions?! 0 Answers
Small 2D collider passing through other thin collider 2 Answers