2D Toolkit Forum

2D Toolkit => Support => Topic started by: corlenbelspar on May 03, 2015, 09:37:19 am

Title: Manual Collision Detection?
Post by: corlenbelspar on May 03, 2015, 09:37:19 am
I'm not using physics because we're trying to make a classic 2D NES stylized platformer.  How do I go about checking collisions with tilemaps in the scripting?
Title: Re: Manual Collision Detection?
Post by: unikronsoftware on May 03, 2015, 12:28:09 pm
You can use tilemap.GetTileIdAtPosition to resolve a tile id from world position.
Title: Re: Manual Collision Detection?
Post by: corlenbelspar on May 04, 2015, 05:35:38 am
Thanks for the prompt reply!  I let the guy know who is teaching me how to program properly, for future reference.
Title: Re: Manual Collision Detection?
Post by: corlenbelspar on May 05, 2015, 03:47:01 am
I have the following code below, but Unity keeps producing this error as the game runs, every frame:
Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
GeneralPhysics.Update () (at Assets/Scripts/Managers/GeneralPhysics.cs:11)

Here is my code:

Code: [Select]
using UnityEngine;
using System.Collections;

public class GeneralPhysics : MonoBehaviour {
private tk2dTileMap tileMap;
private int tileId;

void Update()
{
Vector3 testLocation = new Vector3(transform.position.x,transform.position.y,0f);
tileId = tileMap.GetTileIdAtPosition(testLocation,9);
}
}

How do I get it to stop making this error?  Sorry if it's a noob question, I'm still learning C#.
Title: Re: Manual Collision Detection?
Post by: corlenbelspar on May 06, 2015, 01:39:25 am
Never mind, I got this solved using Raycasting and colliders which I wanted the sprites' collision detection for other sprites and tiles to be the same anyway.
Title: Re: Manual Collision Detection?
Post by: unikronsoftware on May 06, 2015, 10:15:46 pm
The tilemap needs to be attached / got from somewhere.
Eg. public tk2dTilemap tileamp; and Drag a reference to it in the inspector.

Or if its on the same object you can GetComponent as well.
Title: Re: Manual Collision Detection?
Post by: corlenbelspar on May 07, 2015, 04:43:13 am
Oh OK thanks.  Good for future reference I s'pose. :p