- Home /
Tracking distance after Instantiate
Hello.
Im trying to find information about possibility of checking distance between player (represented by classic First Person Character Controller) and Object, which this player has shot. I use simple C# script to shoot in direction.forward. Its done by Instantiate method.
Problem is, that the instantiate method only clones original object, so the distance is always calculated between original object and player.
Do you have any ideas how to calculate it right? :)
Thanks, Erhan
Answer by Mike 3 · Nov 07, 2010 at 02:34 PM
Something like this, where clonedObject is defined outside of any function (same as the prefab is)
Transform clonedObject;
//do your instantiation stuff clonedObject = Instantiate(prefab, whatever, whatever2).transform;
//to check distance Vector3 dist = Vector3.Distance(transform.position, clonedObject.position);
Answer by Erhan · Nov 07, 2010 at 03:28 PM
Yes, this code works fine. But it only detects distance between player and the last instatiated Object. Thats not exactly what I expected. I wanted to track if player is in range of this object. For example, there will be 3 objects and whenever player reaches the point, where is the distance between one of those objects and player 10 it will kill the player.
Im gonna try to add a sphere collider to prefab that I instatiate and calculate it this way. Do you think it might work?
just have a var check the distance between the player and the object when you collide with the sphere collider.