OnTriggerEnter2D not working! Please Help Me!,OnTriggerEnter2D is not working! Please help me!
In my game the character throws a rock at enemies, however the process of the rock colliding with the enemy does not seem to work. Please help me!
void OnTriggerEnter2D(Collision other)
{
if(other.transform.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
},In my game, I throw a rock and try to hit an enemy, however the collision detection between the rock and the enemy does not seem to work. Please help me!
void OnTriggerEnter2D(Collision other)
{
if(other.transform.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
}
Answer by lawrence-parry · Sep 22, 2016 at 11:26 AM
OnTriggerEnter2D uses 'Collider2D' not 'Collision'
if you change your script to this it should work:
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
}
Answer by Stealth_Chicken101 · Sep 22, 2016 at 09:26 PM
The rock now doesn't seem to fire - I don't know why.
public Transform firePoint;
public GameObject rock;
if (Input.GetKeyDown(KeyCode.LeftShift))
{
Instantiate(rock, firePoint.position, firePoint.rotation);
}
public float speed; public Movement player; public GameObject Death;
// Use this for initialization
void Start () {
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
player = FindObjectOfType<Movement>();
if(player.transform.localScale.x < 0)
{
speed = -speed;
}
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Enemy")
{
Instantiate(Death, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Debug.Log("collided");
}
Destroy(gameObject);
}
Now the problem is not so much the collision detection, but the actual firing of the rock. BTW thanks for the help. @lawrence-parry
You need to put your 'if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift))' in the Update function so that it is run each frame. It should look like this:
void Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift)){
Instantiate(rock,firePoint.position, firePoint.rotation);
}
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
}
If this works can you mark my answer as correct ;)
P.S. you don't need to call GetComponent() in the update function, you have already stored it in the 'rigidbody2D' variable so use that in its place. GetComponent has a moderate impact on performance so avoid using it if it isn't absolutely necessary.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093254im_/https://answers.unity.com/themes/thub/images/avi.jpg)