- Home /
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 --;
}
}
}
Answer by Montraydavis · Oct 31, 2012 at 02:16 PM
To get distance, do this...
var TurretDistance : float = (transform.position - Target.transform.position).sqrMagnitude * (transform.position - Target.transform.position).sqrMagnitude ;
Thanks for replying so fast. I will try that when I can get to my computer. Thanks!
You are welcome :) Feel free to mark as answered, or vote up if this ends up working for you. Thanks for using Unity :)
Answer by Tim-Michels · Oct 31, 2012 at 02:34 PM
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 ;)
I also need to limit how far the turrets can see. How would I integrate that?
You could add a member ViewDistance and only perform further checks for attacking if the distance is smaller than ViewDistance.
I hope that covers your question...
Well, first you add a new var in your script: var ViewDistance = 10.0;
Then you Update function should look something like this:
function Update() { distance = Vector3.Distance(Target.position, transform.position); //Calculate distance every frame
if(distance < ViewDistance) //Check if distance is smaller than maximum view distance { //Only perform further checks when the target is in viewing range. //If the target is further than the ViewDistance, its not needed to calculate further stuff 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(); }
A$$anonymous$$$$anonymous$$O = ammo; } } }
Sorry for the bad code formatting, just copy paste it in your script, code doesn't look good in a comment
thumbs up ;)
Okay so I have posted the code that I used from you comment and integrated it into my previous code. Although all it is doing is creating a giant lag machine and won't target anything.
Your answer
Follow this Question
Related Questions
Enemy Jitters when i want him to stop a certain distance 0 Answers
Are Unity distances that small? 1 Answer
Angle of 2 Vector3 on one axis 3 Answers