- Home /
Trigger 2D (another gameObject)
Hello, I want to create a 2D game , I have a problem, I want to do a "OnTriggerEnter2D" where the gameObject attach is not take and take another. Exemple: the script is attached to the player, bullet enter in collision with another player. Sorry for my english, this is the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
[SerializeField]
private Stat health;
[SerializeField]
private Stat energy;
[SerializeField]
private Stat gems;
private void Awake()
{
health.Initialize();
energy.Initialize();
gems.Initialize();
}
void OnTriggerEnter2D (Collider2D bullet)
{
if (bullet.gameObject.tag == "Bullet")
{
health.CurrentVal -= 10;
Debug.Log("works");
}
if (Input.GetKeyDown(KeyCode.S))
{
health.CurrentVal -= 10;
}
if (Input.GetKeyDown(KeyCode.A))
{
energy.CurrentVal += 10;
}
if (Input.GetKeyDown(KeyCode.E))
{
energy.CurrentVal -= 10;
}
if (Input.GetKeyDown(KeyCode.Q))
{
gems.CurrentVal += 10;
}
if (Input.GetKeyDown(KeyCode.D))
{
gems.CurrentVal -= 10;
}
}
}
Answer by OGSynapse · Jun 21, 2017 at 02:26 AM
You should replace OnTriggerEnter2D with OnCollisionEnter2D or OnCollisionStay2D. Hope this works!
Answer by AgarFun · Jun 21, 2017 at 04:07 AM
You don't have understand my problem , my bullet is a trigger so I can't change that. Because of my script is attached to "player" I can't associed more over than 2 gameobjects (principal(player) and other(bullet)) with the method "OnTriggerEnter2D"(and similar). I want detecte a collider2d with enemy and bullet and don't change the sript's place.
I didn't quite understand what you want to achieve. Could you please elaborate.
Answer by altaiirdesmond · Jun 21, 2017 at 04:18 AM
@AgarFun Do you want to damage the enemy upon bullet hit ? Is that right ?
Answer by tanoshimi · Jun 21, 2017 at 06:04 AM
If Object A collides with Object B, the collision message will be sent to both A and B, calling their OnCollisionEnter/OnTriggerEnter callbacks.
However, there is no way for Object C to listen for this collision. Only the objects involved in the collision itself will be notified.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D not working 2 Answers
2D Character slows down in a trigger 2 Answers
Collider other doesnt recognize other gameobjects 1 Answer
Collider lets object through 0 Answers
2D Whether to use a Trigger or not. 1 Answer