… The following code from Unity script reference, it can do the following:
(Find closest object with tag name Enemy, then print its name)
// Print the name of the closest enemy
print(FindClosestEnemy().name);
// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
I want make this change:
instead of Printing the closest object name, Go to its position
Original Line:
print(FindClosestEnemy().name);
I’ll replace with this line:
FindClosestEnemy().transform;
is it right ?
I did it here is my game link:
http://dl.dropbox.com/u/42356492/_Unity/WebPlayer/WebPlayer.html
I added some lines in Update function:
function Update () {
//Enemy go to waypoint position
var WaypointPosition;
WaypointPosition = FindClosestEnemy().transform.position;
transform.position = WaypointPosition;
}
Now, enemy find the nearest point and JUMP to it, how can go to point SMOOTHLY instead of JUMPING ?
Search MoveTowards in the Unity API scripting reference. That will get you in the right direction
Unity - Scripting API: ← thats the link incase you dont know how to get there
Alternatively, burgzergarcade.com
check out episodes Enemy API 1 + 2. That is basically what you need.
… I used MoveTowards – but enemy still jumping not walking toward waypoint, this is my full code:
function Update () {
//Enemy go to waypoint position
var WaypointPositionX;
var WaypointPositionZ;
WaypointPositionX = FindClosestEnemy().transform.position.x;
WaypointPositionZ = FindClosestEnemy().transform.position.z;
//transform.position = WaypointPosition;
transform.position = Vector3(Mathf.MoveTowards
(WaypointPositionX, target, speed * Time.deltaTime), 0, Mathf.MoveTowards
(WaypointPositionZ, target, speed * Time.deltaTime));
}
// Find the name of the closest point
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Waypoint
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Waypoint");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
What is my mistake … ?
Now, when game start, red cube can find nearest waypoint and go to it, but I make some changes in my code to let red cube FACE to waypoint, but there is an error in cube rotation… I need some help:
My Game Link:
http://dl.dropbox.com/u/42356492/_Unity/WebPlayer/WebPlayer.html
My Script:
var endPoint : Vector3;
var duration : float = 1.0;
private var startPoint : Vector3;
private var startTime : float;
function Start () {
startPoint = transform.position;
startTime = Time.time;
}
function Update () {
endPoint = FindClosestEnemy().transform.position;
transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
var newWaypoint : Vector3 = (new Vector3 (endPoint.x, endPoint.y, endPoint.z));
transform.LookAt (newWaypoint);
transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));
}
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Waypoint
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Waypoint");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}