I am working on a standard Top-Down 2D RPG. On my Player object, I have two colliders, one is a regular box collider 2D not marked as trigger handling the collisions and it is smaller than the player vertically. I have another box collider 2D which is marked as trigger, working as a hitbox of sort, and it covers the whole player-. While picking up items, I wanted to use the trigger collider so I used this code :
private void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<DroppedItem>() != null && other.isTrigger)
{
Debug.Log("Picked up item.");
DroppedItem item = other.GetComponent<DroppedItem>();
inventory.AddItem(item.item, item.itemAmount);
Destroy(item.gameObject);
}
}
The debug message is triggered twice each time I collide with the dropped item, which also has a trigger collider, even though I destroy the item afterwards. The debug message is also triggered twice if I mark the trigger collider as non trigger, or vice versa. How do I avoid this?
It seems that you need to have the collider on the player, and the trigger on the item. Then, a script needs to be attached to the item which calls a function on the player which adds items, and then in the script you can destroy the item. The only reason I can think of for why this is being called twice is that your script is on both the item and the player.
(Simple diagram with some pseudocode)
Related
I'm making a game in Unity 2D and was wondering if there was any way to do what I need.
Please check the image of Two items with hitting colliders.
When the player is activating both colliders and then the pick up button is pressed, the player equips both items. I want to disable one of the item's colliders when another item is nearby and then re-enable it when its by itself again. How should I go about doing this? I was thinking using a list but since both weapons use the same scripts (they're prefabs) I think that would create two lists that would serve no purpose and probably crash.
My initial check was to see if another item was nearby, and if so disable its collider, but it did that for both the items rendering both useless.
You can put a Boolean flag in the player code
something like :
if(hasWeapon == false)
{
//put pick up code here
}
so that if a weapon is selected, it will not be entered into the pick-up code until you drop the weapon.
I have a object "A" with non convex mesh collider and kinematic rigidbodie and i wand to check trigger between Object "A" and another Game Object with non convex mesh collider and kinematic rigidbodie
As you see in this collision detection matrix:
Between two Kinematic the trigger is working and giving the message, so if you put both a trigger Collider it will give you a message.
If you want to be sure just add a tag to one and so you can also check the tag first and avoid wrong triggers:
void OnTriggerEnter(Collision other)
{
if(other.compareTag("Trigger2")
// do things
}
I am currently making a sidescrolling game in Unity 2D where an enemy rises up, and then dives down to the player, but then keeps going until it is off screen, like how this image shows. 1 I can get the enemy to collide with the player but I can't make it keeping in that direction until off screen. How would I be able to do this?
You have to make your collider to trigger (it's a checkbox on your collider)
That means your object will not be affected by physics when it touch another collider, but it can calls functions.
You have to replace OnCollisionEnter/OnCollisionEnter2D, etc by OnTriggerEnter/OnTriggerEnter2D, etc.
Be careful, the parameter of the function may change too (Collision/Collider).
I'm developing a simple 2D Unity game (which I'm very new to so sorry if this is a silly question!) in which my player can eat its enemies by colliding with them. This works fine as I'm just selecting the "is trigger" component for the enemies and using this code in my Player class:
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Hit detected");
Destroy(other.gameObject);
transform.localScale += new Vector3(x, y, z);
}
However, this means the colliders placed around the border of my background image aren't stopping the enemies. What's the best fix for this?
I don't understand very well your question. However it seems that your collisions are not working. So, remember that for have collisions actually taking place in your game you need to use colliders and that one of the two elements participating in the collision need to have the rigidbody component.
That will make the physics work in the engine, which triggers dont.
To check if that works you can debug with:
// called on collision
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("OnCollisionEnter2D");
}
From what I understood, you want only to detect triggers between the player and the enemies, but you still want these to collide with physic objects in your scene, such as background colliders.
One possible way to achieve this is to create a child object for the player object with a collider component with the trigger option set, and attaching a script to it to handle the triggers. Then, with the use of layers to group your player and enemy objects, you can uncheck the collision between them following: Edit -> Project Settings -> Physics 2D: "Layer Collision Matrix".
You can assign a script to any enemy, checking the distance with the player in each frame. Then you can Uncheck "is trigger"
Vector2.Distance
So I'm making a simple "puzzle" using lights. Simply, there are 3 buttons with lights on them (red, blue, green). Each button has it's own trigger volume but when I go to play, nothing prints that I even enter, stay, or leave the trigger. I've never used Collider Variables before so I feel like I'm doing something wrong (obviously or it would be working!). But then I just did "Collider entity" in the OnTriggerStay/Enter/Exit Method and it still didn't print to the console that my player was entering. Why are my Triggers not working?
Click here for the code I'm trying
Click here to see how I have it in the Unity Scene
Triggers only respond to other colliders that have rigid bodies on them.
Try adding a Ridgidbody component to your player and set it to kinematic.
OnTriggerEnter/Stay/Exit works when the Object has a Collider Component and BluePuzzle2 doesn't have that.
Also OnTrigger function gets a Collider as parameter. Check the reference page
So in order to make that work put a script on every light and on that script copy this function
void OnTriggerEnter(Collider col) {
if (col.CompareTag("Player")) {
print("Entered the trigger");
}
}
Hope it helps.