Hello Guest

Author Topic: Collision for Sprites/Tilemaps  (Read 5064 times)

Sovo

  • Newbie
  • *
  • Posts: 5
    • View Profile
Collision for Sprites/Tilemaps
« on: January 07, 2014, 06:22:00 am »
I have spent a few hours trying to get OnCollisionEnter or OnTriggerEnter to fire on a sprite's script without success.  The project composition is extremely basic.  Just a tilemap and two sprites.  The tilemap has a collider via the sprite collection settings, but no rigidbody.  The two sprites have colliders, also via the sprite collection settings, and both have rigidbodies.  I have increased the collider depth to 10 on all colliders and set their z-axes to 0.

My script looks like this:
Code: [Select]
public class PlayerScript : MonoBehaviour {

// Use this for initialization
void Start () {
        Debug.Log("started");
}

// Update is called once per frame
void Update () {
        transform.Translate(-10f * Time.deltaTime, 0, 0);
}

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("collision entered");
        GameObject.Destroy(this);
    }
    void OnTriggerEnter (Collider other) {
        Debug.Log("trigger entered");
        GameObject.Destroy(this);
    }
}

Do any problems jump out at anyone?  I appreciate any help and advice you can offer in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision for Sprites/Tilemaps
« Reply #1 on: January 07, 2014, 10:46:01 am »
You can't use transform.Translate to move objects - that will break physics. Also, you should use FixedUpdate for any physics related updates. The sprites rigidbody should be set to kinematic, and maybe use rigid body.MovePosition.

Sovo

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Collision for Sprites/Tilemaps
« Reply #2 on: January 07, 2014, 05:12:21 pm »
Thank you for the reply.  The sprite's rigidbody is set to kinematic.  I'll try using the MovePosition method instead of the transform.translate method.  I've been following a few tutorials, and they're all using transform.translate, but they haven't been working with colliders, yet.

Sovo

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Collision for Sprites/Tilemaps
« Reply #3 on: January 08, 2014, 03:25:38 am »
Okay.  I have made the changes you recommended. 

Code: [Select]
public class PlayerScript : MonoBehaviour {

// Use this for initialization
void Start () {
        Debug.Log("started");
}

// Update is called once per frame
void FixedUpdate () {
        rigidbody.MovePosition(rigidbody.position + new Vector3(-10f * Time.deltaTime, 0, 0));
}

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("collision entered");
        GameObject.Destroy(this);
    }
    void OnTriggerEnter (Collider other) {
        Debug.Log("trigger entered");
        GameObject.Destroy(this);
    }
}

Movement is happening with ostensibly the same effect as transform.Translate.  However, the collision events are still not firing.  I'm going to try starting a project from scratch and document every step I take.  In the meantime, here's a screen of the editor, in case anything jumps out at someone.


Sovo

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Collision for Sprites/Tilemaps
« Reply #4 on: January 08, 2014, 04:32:37 am »
After some more testing, I can get the player sprite to trigger a collision with the other sprite by setting it to a box collider in the sprite collection editor.  However, polygon collider still does not fire a collision event.  Nor does the tilemap mesh collider.  I have a feeling this has something to do with mesh colliders, but I'm the only one who seems to be having a problem.  I must have missed something very basic.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collision for Sprites/Tilemaps
« Reply #5 on: January 08, 2014, 11:53:10 am »
you didn't say you were using mesh colliders. Concave mesh colliders can't collide with other concave mesh colliders in Unity. Use box / convex mesh colliders, or use the 2D physics system which doesn't have this limitation.

Sovo

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Collision for Sprites/Tilemaps
« Reply #6 on: January 08, 2014, 07:52:08 pm »
That seems to be the issue and why none of my collisions were working.  Thanks again for your help.