- Home /
onCollisionEnter vs onTriggerEnter
I'm finding it difficult to decide which one to use and what triggers such events. I have looked at the API reference for both methods extensively and I've done some testing and I've got some confusing results.
using UnityEngine;
using System.Collections;
public class PlayerShot : MonoBehaviour
{
public int speed;
void Start ()
{
speed = 15;
}
void Update ()
{
transform.Translate (0, 1 * speed * Time.deltaTime, 0);
}
public void onCollisionEnter()
{
Debug.Log("onCollisionEnter() called");
Destroy(gameObject);
}
}
This is my script that is attached to the "bullet" that gets fired. As I understand it, you only need a rigidbody attached to both objects involved yes? I have a empty game object that is off screen with a box collider attached to it. When the "bullet" passes through the collider it should trigger the method correct? Mine just pass through. Is there something I'm doing wrong here? Or should I be using onTriggerEnter instead?
Answer by superluigi · Jan 26, 2014 at 12:20 AM
From my understanding Translate means that it'll ignore collisions. That is your problem. To give you an example if you Translated your character down he would go through the floor. So
function FixedUpdate()
{
rigidbody.velocity = Vector2(0, speed, 0);
}
If it has a rigidbody you should put it in FixedUpdate. You don't need to multiply by Time.deltaTime because it runs at the same time step as FixedUpdate. However, when working with rigidbodies usually you want realistic physics and manipulating the velocity directly like I just did will lead to unrealistic physics. It will work exactly like translate did though and if you were happy with that then this will work for you. If any part of my answer confused you (maybe some of the terms) let me know in a comment and I will explain it.
Also no you only need ONE rigidbody attached to one of the objects. If both have it a rigidbody that'll work too, the general rule is that at least one of the objects should have a rigidbody that is NOT kinematic.
You have been very helpful and I'll try some stuff out today. I'm doing a Galaga style game so I'm just concerned about how fast the "bullet" is traveling. The collision comes into play because I'm destroying the object the moment it goes off screen and hits the box collider. Perhaps there's a better way to implement this? I have yet to add in the condition of what happens if the bullet hits another ship but I would assume its very similar. Thank you.
Are you by any chance using the 2Drigidbodies that Unity just added. If you are then there is another reason the collisions aren't being detected but it's an easy fix. If you are using regular rigidbodies then everythings ok. If you are doing a galaga type game the best thing in my limited experience is to have your ship stay in one spot and have the background move down or to the sides to give off the appearance that the ship is moving. In these situations a collider/trigger is definitely a good idea because everything is working in a confined and controlled space. Also if my answer is correct please mark it as correct. I've only had 2 answers marked as correct and I want more.
Your code worked somewhat. I think I had to tweek it a little to get the effect I was looking for but overall it steered me in the correct direction. As far as movement goes well that's a little tricky because I want it set up like a game called Gryus (I think that's how you spell it) If you google it you can see that the movement and the way the game is set up is a little bit tricky.
Would the code that you have posted for moving a rigidbody work on the 2d version? I don't have 2d rigidbodies on my objects. Is there a difference?