N
N
NickName73312018-04-24 18:32:11
C++ / C#
NickName7331, 2018-04-24 18:32:11

Unity(C#) - Why is the bullet not destroyed when touching the collider?

By pressing a certain button, the character shoots a bullet, it must be destroyed on contact with any collider other than the owner's collider (so that the bullet is not destroyed when it spawns). The bullet is fired but not destroyed when passing other colliders. What is the problem? Here is the code:

private GameObject parent;  //Хозяин пули
  public GameObject Parent { set { parent = value; } get { return parent; } }

  private float speed = 10.0F;
  private Vector3 direction;
  public Vector3 Direction { set {direction = value; } }

  public Color Color
  {
    set { sprite.color = value; }
  }

  private SpriteRenderer sprite;

  private void Awake()
  {
    sprite = GetComponentInChildren<SpriteRenderer>();
  }

  private void Start()
  {
    Destroy (gameObject, 1.4F);
  }

  private void Update()
  {
    transform.position = Vector3.MoveTowards (transform.position, transform.position + direction, speed * 
        Time.deltaTime);
  }


  private void OnTriggerEnter2D(Collider2D collider) 
  {
    Unit unit = collider.GetComponent<Unit> ();

    if (unit.gameObject != parent) //Если игровой объект не является родителем(хозяином)
    {
      Destroy (gameObject);
    }
  }

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2018-04-24
@NickName7331

OnTriggerEnter2D works with triggers, OnCollisionEnter2D - with colliders. Decide first.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question