3D Platfomer Targeting Script HELP

I absolutely cannot understand this for the life of me. The pourpose of this script is when the player is near an enemy with the tag “Enemy”, AND the player is holding down “Fire2”, AND the enemy is on screen, the musket and head will be able to look at the enemy, and a crosshair is supposed to stay over the enemy it’s near, but that’s not the case here. I don’t know what to put in the places of the code that has enemy here. Do I have to FindGameObjectsWithTag and do something like that? I believe I tried that and it didn’t work. If anyone can help me, that be appreciated. NOTE: Every enemy in the world is the same, same tag, same AI. I’m really copying and paste the enemies everywhere once I drag a single prefab out.

var musket : Transform;
var crosshair : Transform;
var head : Transform;
var distance = 20.0;


function Update(){
		if(Vector3.Distance(*enemy here*.position, transform.position < distance  Input.GetButtonDown("Fire2")  *enemy here*.renderer.isVisible){
			musket.transform.LookAt(*enemy here*);
			head.transform.LookAt(*enemy here*);
			crosshair.guiTexture.enabled = true;
			crosshair.transform.position = Camera.main.WorldToViewportPoint(*enemy here*);		
		else{
			musket.transform.localRotation = Quaternion.identity;
			head.transform.localRotation = Quaternion.identity;
			crosshair.guiTexture.enabled = false;			
		}
}

Yes you need to find all the enemies first then loop through them. Something like this should work.

var EnemyArray : GameObject[];

function Start(){

       EnemyArray = GameObject.FindGameObjectsWithTag("Enemy");

}

Then you’ll have to loop through EnemyArray. Add this in your Update function or call a coroutine.

for(var i = 0; i < EnemyArray.length(); i++){

    if(Vector3.Distance(EnemyArray[i].transform.position, transform.position < distance  Input.GetButtonDown("Fire2") &EnemyArray[i].transform.renderer.isVisible){

            musket.transform.LookAt(EnemyArray[i].transform);

            head.transform.LookAt(EnemyArray[i].transform);

            crosshair.guiTexture.enabled = true;

            crosshair.transform.position = Camera.main.WorldToViewportPoint(EnemyArray[i].transform);     

        else{

            musket.transform.localRotation = Quaternion.identity;

            head.transform.localRotation = Quaternion.identity;

            crosshair.guiTexture.enabled = false;          

        }
}

Not perfect, but should get you on the right path.

It took care of the past problems, but now it brought up some new ones I don’t understand:

  1. (13,34): BCE0077: It is not possible to invoke an expression of type ‘int’.
    2)(14,85): BCE0051: Operator ‘<’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘float’.
    3)(18,88): BCE0017: The best overload for the method ‘UnityEngine.Camera.WorldToViewportPoint(UnityEngine.Vector3)’ is not compatible with the argument list ‘(UnityEngine.Transform)’.
var enemy : GameObject[];
var musket : Transform;
var crosshair : Transform;
var head : Transform;
var distance = 20.0;

function Start(){
	enemy = GameObject.FindGameObjectsWithTag("Enemy");
}


function Update(){
	for(var i = 0; i < enemy.length(); i++){
		if(Vector3.Distance(enemy[i].transform.position, transform.position < distance  Input.GetButtonDown("Fire2")  enemy[i].transform.renderer.isVisible)){
			musket.transform.LookAt(enemy[i].transform);
			head.transform.LookAt(enemy[i].transform);
			crosshair.guiTexture.enabled = true;
			crosshair.transform.position = Camera.main.WorldToViewportPoint(enemy[i].transform);   
		}
		else{
			musket.transform.localRotation = Quaternion.identity;
			head.transform.localRotation = Quaternion.identity;
			crosshair.guiTexture.enabled = false;
		}
	}
}
if(Vector3.Distance(enemy[i].transform.position, transform.position < distance ......

should be

if(Vector3.Distance(enemy[i].transform.position, transform.position) < distance ......

also

crosshair.transform.position = Camera.main.WorldToViewportPoint(enemy[i].transform);

should be

crosshair.transform.position = Camera.main.WorldToViewportPoint(enemy[i].transform.position);

Although I’m not too sure about WorldToViewPoint reference might need some more reading…

You might get more reply’s if you posted in the scripting forum.

I only got one reply, and it’s the same as you put. All I had a problem with was the (enemy*.transform.position), and some of my variables were put in from the project prefabs, and not the objects in the hiearchy.*
So now, it’s just about complete. All that needs to be done is to reset the player to his regular state when he destroys an enemy. So I tried this:
```

  •   if(jesus.transform == null){
      	crosshair.guiTexture.enabled = false;
      	musket.transform.localRotation = Quaternion.identity;
      	head.transform.localRotation = Quaternion.identity;
      }*
    

```
Didn’t work…but I’ll see if I can find something.
EDIT: This script works, but not for multiple enemies. It only targets the first copied enemy, then it’ll target the next copied enemy. Perhaps if it was more like “target the enemy that meets the conditions AND which one is closest to the player.”

Lol you’re main character is jesus? Or are you shooting jesus? Either way that’s pretty awesome :slight_smile:

lol. naw, I just put in a random variable. I couldn’t think of anything but jesus. I love jesus btw lol:)