- Home /
Weapons Range Projectile script
So i'm trying to make it so when my enemy ship is within a certain range of the Tag "Player" that it fires a projectile at the player tag, but at the moment it's just firing at any range
var projectile:Rigidbody;
var TorpedoSpeed:float = 0.0;
var shotDelay:float = 3;
var lastshot:float = Time.time;
private var launchLocation:Vector3;
launchLocation = Vector3(transform.position.x,transform.position.y,transform.position.z);
function Update()
{
if ((Time.time - lastshot) > shotDelay) {
TargetObj = GameObject.FindGameObjectWithTag("Player");
lastshot = Time.time;
var dist = Vector3.Distance(TargetObj.transform.position, TargetObj.transform.position) < 10;
GetLaunchLocation();
var instantiatedProjectile:Rigidbody = Instantiate(projectile,launchLocation,transform.rotation);
instantiatedProjectile.transform.Rotate(Vector3(0,0,0));
instantiatedProjectile.velocity = transform.forward * TorpedoSpeed;
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);
}
}
function GetLaunchLocation()
{
launchLocation = Vector3(transform.position.x,transform.position.y,transform.position.z);
}
Also this is my first time using Unity Answers so please forgive me if i formatted my code wrong.
That doesn't work for what i need it to do, i need the delay timer in there and i also need it to be under 10 units, not over. i can easily modify the over to under thing.
The way this works is that if the player is more than 10 units away, the projectile will not fire. The timer still runs, but it may fire immediately the first time the player is back in range after having left the range.
Answer by robertbu · Feb 20, 2013 at 05:05 AM
Change line 11 above to:
if (Vector3.Distance(TargetObj.transform.position, transform.position) > 10)
return;
Your answer
Follow this Question
Related Questions
Render 3D object to a 2D texture at runtime 1 Answer
Place stars (prefabs) in random locations 3 Answers
Distance Changing Conversions (1000m = 1km 1200m = 1.2km etc) 3 Answers
Warp drive and sound on key press 0 Answers
How to make a universe scenery 2 Answers