Mesuring distance through script

I have a turret script that in its previous version would use a trigger to judge if it was to shoot at an enemy. That worked okay, but enemies would have to enter the trigger one by one. I want it so that it uses a script to decide if an enemy is in range. I believe that this will solve my issue.
Thanks!

 var distance;
    var Look : boolean = false;
    var Target : Transform;
    var damp : float;
    var Projectile : Transform;
    var Shell : Transform;
    var MuzzleFlash : Transform;
    var fireRate : float =  5;
    var Speed : int = 2000;
    var ShellSpeed : int = 500;
    var TurretAudio : AudioClip; 
    var TurretAnimation : AnimationClip;
    static var ammo : int = 250;
    var AMMO : int;
    var AttackDistance = 10.0;
    
    private var nextFire = .5;
    
    function Update()
    {
    	distance = Vector3.Distance(Target.position, transform.position);
    	
    	if(distance < AttackDistance)
    	{
    		if(gameObject.tag == "Enemy")
    		{
    			Look = true;
    			if(!Target)
    			{
    				Target = gameObject.transform;
    			}
    		}
    	 
    	if(Look && Target != null)
    	{
    		var rotate = Quaternion.LookRotation(Target.position - transform.position);
    		
    		transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
    		Shoot();
    	}
    	
    	AMMO = ammo;
    	}
    }
    
    function Shoot()
    {
    	if(ammo > 0)
    	{
    	if(Time.time > nextFire)
    	{
    		nextFire = Time.time + fireRate;
    			
    			var muzzleflash = Instantiate(MuzzleFlash, transform.Find("Spawn").transform.position, transform.rotation);
    			
    			var projectile = Instantiate(Projectile, transform.Find("Spawn").transform.position, transform.rotation);	
    			projectile.rigidbody.AddForce(transform.forward * Speed); 
    			
    			var shell = Instantiate(Shell, transform.Find("Spawn_2").transform.position, transform.rotation);
    			shell.rigidbody.AddForce(transform.right * ShellSpeed);
    			
    			audio.PlayOneShot(TurretAudio);
    			animation.Play(TurretAnimation.name);
    		ammo --;
    	}
    }
    }

REVSIDED CODE:

 var Look : boolean = false;
    var Target : Transform;
    var damp : float;
    var Projectile : Transform;
    var Shell : Transform;
    var MuzzleFlash : Transform;
    var fireRate : float =  5;
    var Speed : int = 2000;
    var ShellSpeed : int = 500;
    var TurretAudio : AudioClip; 
    var TurretAnimation : AnimationClip;
    static var ammo : int = 250;
    var AMMO : int;
    var ViewDistance = 10.0;
    var AttackDistance = 8.0;
    
    private var nextFire = .5;
    
    function Update()
    { 
    	distance = Vector3.Distance(Target.position, transform.position);
    	if(distance < ViewDistance)
    	{ 
    	
    	if(distance < AttackDistance)
    	{ 
    	
    	if(gameObject.tag == "Enemy")
    	{ 
    		Look = true; 
    		if(!Target) 
    		{
    			Target = gameObject.transform; 
    		} 
    	}
    
    	if(Look && Target != null)
    	{ 
    		
    		var rotate = Quaternion.LookRotation(Target.position - transform.position);		
    
    		transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
    	
    		Shoot(); 
    	}
    
    			AMMO = ammo; 
    		} 
    	} 
    }
    
    function Shoot()
    {
    	if(ammo > 0)
    	{
    	if(Time.time > nextFire)
    	{
    		nextFire = Time.time + fireRate;
    			
    			var muzzleflash = Instantiate(MuzzleFlash, transform.Find("Spawn").transform.position, transform.rotation);
    			
    			var projectile = Instantiate(Projectile, transform.Find("Spawn").transform.position, transform.rotation);	
    			projectile.rigidbody.AddForce(transform.forward * Speed); 
    			
    			var shell = Instantiate(Shell, transform.Find("Spawn_2").transform.position, transform.rotation);
    			shell.rigidbody.AddForce(transform.right * ShellSpeed);
    			
    			audio.PlayOneShot(TurretAudio);
    			animation.Play(TurretAnimation.name);
    		ammo --;
    	}
    }
    }

Well if you simply want the distance between 2 points, there’s a simpler option:

float distance = Vector3.Distance(transform.position, Target.transform.position);

This should be the simplest and quickest way of getting the distance.

Cheers :wink:

To get distance, do this…

var TurretDistance : float = (transform.position - Target.transform.position).sqrMagnitude * (transform.position - Target.transform.position).sqrMagnitude ;