I cant get 2D collisions to work, but everything looks ok to me, any help please?
I am very new to unity and programming genrally but im trying to make a frogger like game, i cant get my player to hit any enemies, heres my script...
using UnityEngine; using System.Collections;
public class DeathTrigger : MonoBehaviour {
void OnCollisionEnter (Collision col){
if (col.gameObject.tag == "Enemy")
{
print ("Hit!");
}
}
}
From all the online stuff ive read this should work, but im getting no joy, ive got two objects, both with box colliders 2D and both have rigidbody 2D's. Im probably missing something super simple but all the same...
This has been bugging me for a while and any help is more than welcome, thanks.
P.S. Using Unity Ver: 5.4.1 and writing in Csharp.
I have also recently tried setting the collisions to look for the name of the object being collided with rather than the tag to no avail...
Answer by UnityCoach · Dec 10, 2016 at 07:11 PM
If I remember well, the documentation is misleading, or the Collision2D model is working like expected.
The .gameObject property will return the gameObject the component is on. Try using .collider.gameObject instead.
void OnCollisionEnter (Collision col)
{
if (col.collider.gameObject.tag == "Enemy")
{
print ("Hit!");
}
}
Answer by rjs_chipmunk · Dec 10, 2016 at 10:43 PM
Thanks for the reply but it still doesnt work, like i said i am new to this, so are there any inspector settings i might have wrong?
The 2 screenshots are of the 'truck' or the enemy, and of the player.
Thanks again.
For a collision to happen, none of the two must be set to trigger. Otherwise, they OnCollisionEnter will never be received, and you should use OnTriggerEnter.
Also, I just realise you work in 2D. You must use OnCollisionEnter2D ().
Answer by kynian · Dec 11, 2016 at 08:57 AM
One is set as a rigidbody collider, the other is set as a static trigger collider. This means you would need to handle triggers not colliders. So you would want either OnTriggerEnter or OnTriggerEnter2D. Look at this link for a chart on which setups will cause collisions vs triggers: https://docs.unity3d.com/Manual/CollidersOverview.html
i changed the triggers and now when i hit the truck my player sort of bounces around him, so the colliders are recognising eachother but its still not doing anything beyond that...
thanks for the reply.
i get this console error when i change the 'OnCollisionEnter' to an 'OnTriggerEnter' (it wouldnt let me upload an image, but the error is this... "Script Error:OnTriggerEnter This message parameter has to be of type:Collider") It runs fine in the script editor, i just get the error 'in game' as it were.
That's right. The error your giving gives you a hint. In this case ins$$anonymous$$d of passing in a collision you're passing in a collider. So it should be something like OnTriggerEnter(Collider collider).
See this documentation for more information: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Also note if you're working in 2d there is also a method called OnTriggerEnter2D(Collider2D collider) that you can use as well.
Answer by brunocoimbra · Dec 11, 2016 at 12:43 PM
You are using OnCollisionEnter(Collision col)
, use OnCollisionEnter2D(Collision2D col)
instead.
Your answer
Follow this Question
Related Questions
Issues with 2D collision/overlap detection,Help detecting 2D collision/overlap 0 Answers
Issue with 2D collision/overlap detection 0 Answers
Projectile colliding with player at origin,Excluding a specific game object 0 Answers
OnCollisionEnter2D not calling, but objects are colliding. 1 Answer
Detect whether a Rigidbody collided with a Trigger 2 Answers