2D Toolkit Forum
2D Toolkit => Support => Topic started by: Sovo 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:
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.
-
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.
-
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.
-
Okay. I have made the changes you recommended.
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.
(http://i42.tinypic.com/a15v0j.png)
-
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.
-
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.
-
That seems to be the issue and why none of my collisions were working. Thanks again for your help.