Why are my bullets hitting my player OR not disappearing when they touch an object

Viewed 37

I am trying to code a basic version of a game be and my friends are making.

the problem is that when the player shoots a bullet when moving the bullet will either hit the player and disappear (If Istrigger on the bullet box collider is ticked) OR (If Istrigger is not ticked) the bullet will stop moving when they touch the environment.

I have a collision ignore script to stop the bullet from hitting my player

Bullet script

using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 20f;
    public Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb.velocity = transform.right * speed;
    }

    void OnTriggerEnter2D(Collider2D hitInfo)
    {
        Debug.Log(hitInfo.name);
        Destroy(gameObject);
    }

}

Ignore Scrip

using UnityEngine;
using System.Collections; 

public class ignore : MonoBehaviour
{

    public GameObject playercollision;
    public GameObject bulletcollision;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void LateUpdate()
    {
        Physics2D.IgnoreCollision(playercollision.GetComponent<Collider2D>(), bulletcollision.GetComponent<Collider2D>());
    }
}
1 Answers

is the bullet spawning touching the player? that may be why, since i had encountered this problem when i was making a game like this

Related