- Home /
Look at issues
Hello guys! I use this script below for automatic aiming at the nearest target. It works briliant, but i have one issue, - my character also must have tag "Player1", and of course if i set it - he begins to aim into itself. How can i make, that my character aim to all other target with tag "Player1", excluding himself? Or maybe exist better way get similar effect?
function Update() {
var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("Player1");
var closest: GameObject;
var closestDist = Mathf.Infinity;
for (waypoint in waypoints) {
var dist = (transform.position - waypoint.transform.position).sqrMagnitude;
if (dist < closestDist) {
closestDist = dist;
closest = waypoint;
}
}
transform.LookAt(closest.transform);
Answer by Geo.Ego · Jan 10, 2013 at 04:53 PM
The correct way to do this is to have a different tag for your targets. The reason that you tag objects is to tell them apart; when you tag them all the same, you'll treat them all the same. Make your player "Player1" and give the targets another tag; adjust your script accordingly.
Ok, can you say, how i can fix error "IndexOutOfRangeException: Array index is out of range.LookAt.Update () (atAssets/Weapon/Script/LookAt.js:19)" it appeared afterward i destroy my target, obviously i must check destroyed my target already, or not, but how?)
I can't say precisely without seeing your code, but that exception refers to an attempt by your code to reference an array index that is outside of the range of the array. For instance, if you four events in your array and you try to access the fifth, you will get this exception. Look for somewhere that your code has the potential to do that. If you are still having the issue, start a new question with as much detail as possible.
I can't say precisely without seeing your code, but that exception refers to an attempt by your code to reference an array index that is outside of the range of the array. For instance, if you four events in your array and you try to access the fifth, you will get this exception. Look for somewhere that your code has the potential to do that. If you are still having the issue, start a new question with as much detail as possible.
Your answer
Follow this Question
Related Questions
Make the turret automatically rotate to look at where the crosshair is aimed at. 3 Answers
How to ensure multiple weapons fire towards the same target? 1 Answer
Look at +1z of target and move to -1z of target? Almost there! 2 Answers
Look at target and flip 1 Answer
target tracking with lead time 1 Answer