B
B
BreakTheSystem2018-07-13 13:29:41
Unity
BreakTheSystem, 2018-07-13 13:29:41

Why is the object not destroyed when touched?

Box Collider set up, object specified

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

  public GameObject Car;
    void Start () {
  }
  
  
  void Update () {
    if (Vector3.Distance(Car.transform.position,transform.position) < 1f)
    {
            Destroy(Car);
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2018-07-13
@BreakTheSystem

Well, colliders have nothing to do with it at all - you don't use them in your code.
And not destroyed perhaps because the distance never becomes <1. The distance is measured from the center of the object - and if each object you have is 1 - then the distance will be <1 when they are half inside each other.
The way out is to use colliders.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question