Can't get Collision to detect triggers in unity 2d
I've been trying for a while to get the players collision to be detected against the objects but alas nothing has worked, and scrubbing through forum responses hasn't helped either. Either I have made a meager mistake or am doing something entirely wrong.
I have a player that has a rigidbody2d, is Dynamic (I've tried making it kinematic and using full kinematic as well) and is simulated. Here is a picture: http://imgur.com/u2y9CfE
The Objects that it is colliding with have these settings: http://imgur.com/EZEXSFd Yes, I've tried not having a rigid body on them, and also have set them to Use Full Kinematic before as well when I do have them as a kinematic rigid body. It makes no difference, I still get no input on collision.
For the code for movement I've tried having it move through rb.Addforce and translating the objects position, both through fixed update.
I've checked the collision Matrixes and it seems to be in order as well, but here is a picture of that in case it may be messed up: http://imgur.com/kcCnt12
The Script for detecting that is connected to the player is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RowCollider : MonoBehaviour
{
public static bool alive;
void Start ()
{
alive = true;
}
void OnTriggerEnter2D (Collider2D other)
{
Debug.Log ("You Collided!");
alive = false;
}
void FixedUpdate ()
{
Debug.Log (alive);
}
}
I really have no idea how to get this to work, I've tried so many things, please help :'(
Whenever the player object collides with the rows it just changes nothing, neither of the debug messages go off.
Answer by Lord_Arcanis · Apr 04, 2017 at 11:58 PM
Guys my bad, Apparently the collider boxes all shifted up for some reason when i made them prefabs, I had no idea :'(.
Answer by Commoble · Apr 03, 2017 at 02:06 AM
You have that script attached to the player object, correct? That's your problem.
When a script has an OnTriggerEnter2D event function, that event is called when a trigger collider on the same object as the script is entered by another object (not when the script's object enters a trigger collider). You need to put your trigger enter function on the trigger area object, not the player object.
Doesn't change a thing, Alive still never gets set to false and a the message "You Collided" never shows up.
Answer by Lord_Arcanis · Apr 04, 2017 at 11:33 PM
Fixed things up a little, but hasn't changed anything: script : http://imgur.com/yyc4pl1
Object that is being collided against: http://imgur.com/NXmmqaU
Collision Matrix: http://imgur.com/DCsJpr5
Basically all that was changed was that the script was moved to the object being collided against rather than being on the player, and the player still has a dynamic rigid body and a box collider that isn't a trigger.
Yet it appears that the script won't even fire, regardless of anything to do with static variables it should at least send the debug message "You Collided!"
The player movement script, that is connected to the player works through rb. Addforce in a manner as so
rb.AddForce (transform.right * 1.5F);
If anyone knows what I'm doing wrong, please notify me.
Answer by Max_power1965 · May 24, 2020 at 03:49 PM
I also had really a lot of problems when configuring the collision between game objects. After years I wrote every tips in this article https://gamedevelopertips.com/unity-collision-detection-2d/, I'm sure it'll be helpful if you want to set up properly you collider/triggers properly.