Hi,
I am working on a script that is supposed to find the closest object to a group of objects in the game ... Of course with determining the type of these objects using another script ... In any case it works well but it chooses the body that has the script as the closest object ... which is Something logical ... but I want to choose any of the available objects except the one that has the script
Important note: I also want to make the object carrying the script the same type of object that must be chosen the closest one in it
public void findTheNearestObject()
{
float distanceToTheNearestobject = Mathf.Infinity;
objects1 nearestObject = null;
objects1[] allObjects = GameObject.FindObjectsOfType();
foreach(objects1 currentObjects in allObjects)
{
float distenceToObjects = (currentObjects.transform.position - this.transform.position).sqrMagnitude;
if (distenceToObjects < distanceToTheNearestobject)
{
distanceToTheNearestobject = distenceToObjects;
nearestObject = currentObjects;
this.transform.position = (this.transform.position - nearestObject.transform.position).normalized * dist + nearestObject.transform.position;
}
}
Debug.Log("the nearst object is :" + nearestObject);
Debug.DrawLine(this.transform.position, nearestObject.transform.position);
}
↧