Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question