Hello Guest

Author Topic: Manual Collision Detection?  (Read 4654 times)

corlenbelspar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Manual Collision Detection?
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Manual Collision Detection?
« Reply #1 on: May 03, 2015, 12:28:09 pm »
You can use tilemap.GetTileIdAtPosition to resolve a tile id from world position.

corlenbelspar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Manual Collision Detection?
« Reply #2 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.

corlenbelspar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Manual Collision Detection?
« Reply #3 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#.
« Last Edit: May 05, 2015, 04:27:36 am by corlenbelspar »

corlenbelspar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Manual Collision Detection?
« Reply #4 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Manual Collision Detection?
« Reply #5 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.

corlenbelspar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Manual Collision Detection?
« Reply #6 on: May 07, 2015, 04:43:13 am »
Oh OK thanks.  Good for future reference I s'pose. :p