- Home /
Script so that, my enemies only see my player at a certain range.
Hi everyone,
I've been following the Tornado Twins tutorials on youtube and i just reached a snag.
In my scene i have turrets, whose head portion rotates along the base in order to aim at my player character. At the moment the turrets will aim and fire at my player character no matter where he is in game.
What i would like is for the Turrets to be offline or not working until the player character or TARGET has come within a certain range.
Thx.
This is my Script so Far
-------------------------------------------------------------------------------------------
var LookAtTarget:Transform; var damp = 6.0; var bulletPreFab:Transform; var savedTime=0;
var attackRange = 30.0; var shootAngleDistance = 10.0; var target : GameObject;
function Update() { }
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//Dampin/Delay on speed at which it follows the target
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time; var oddeven = (seconds % 2);
if(oddeven) {
Shoot(seconds); } }
//Turret Shoot function
function Shoot(seconds) { if(seconds!=savedTime) { var bullet = Instantiate(bulletPreFab, transform.Find("SpawnPoint").transform.position, Quaternion.identity); //Bullet speed bullet.gameObject.tag = "enemyProjectile"; bullet.rigidbody.AddForce(transform.forward * 800);
savedTime=seconds; } //Removed this peice of code, to make it more complex //transform.LookAt(LookAtTarget); //if an enemy as further than maxDistance from you, it cannot see you
}
Could you fix the code segments of your question? It's difficult to read what's going on with the formatting the way it is.
Answer by Shawn · Feb 24, 2010 at 06:47 PM
You would just want to do a distance check to the target... Something like Vector3.Distance(LookAtTarget.position, transform.position) should do the trick. This will return a float of the distance between the 2 points; compare that distance to your desired distance.
Answer by burnumd · Feb 24, 2010 at 08:34 PM
If your player has a rigidbody and is tagged "Player" (if you're using the default FPS Walker prefab, it already has these things), you can do the following:
Add a sphere collider to your turrets (make sure you "Add" and don't "Replace" any other colliders you have on the turret).
Make the sphere collider a trigger.
Make the radius equal to your desired detection distance.
Add the following to your turret script:
function OnTriggerEnter (other : Collider) { if (other.CompareTag("Player")) { // lookAtTarget corresponds to the LookAtTarget variable you defined. // In Unityscript, the preferred style is to start variables with a lowercase. lookAtTarget = true; } }
function OnTriggerExit (other : Collider) { if (other.CompareTag("Player")) { lookAtTarget = false; } }
Letting Unity's collision system handle distances is preferable to checking the distance every frame (see Unity's Optimization Docs).
Place it anywhwere outside your Update function inside the turret script. These are both separate functions you can implement in any script and if a collider is attached to the same object, these functions are automatically called when a trigger event occurs (see the chart at the bottom of this page for the conditions: http://unity3d.com/support/documentation/Components/class-BoxCollider.html ).
I was having trouble with the same script and used your script but get this: Assets/scripts/TurretControl.js(29,32): BCE0022: Cannot convert 'boolean' to 'UnityEngine.Transform'.
Answer by Whatee · Feb 24, 2010 at 07:08 PM
hehe, Thx for the help.
Just not exactly sure where i would put that in?
When replying to an answer, please use the "add comment" link rather than posting a new "answer" to the question.
You can put it wherever works best for your script. $$anonymous$$aybe where you select the LookAtTarget just do a check before assigning that value.
Answer by Grimlock257 · Nov 15, 2010 at 10:13 PM
I finished there tutorials too, here is my script with a few added bits, it looks at you but dost fire until your in range, you can mess around with these settings if you like, if you have not solved you problem then copy this, if your solved then OK then
//Movment and Target
var LookAtTarget:Transform; var damp = 1.7; var ememy : Transform;
//Shooting var bullitPrefab:Transform; var savedTime=0; var curShoot = true;
function Update () { if(LookAtTarget) {
if ( Vector3.Distance( ememy.position, transform.position ) < 18 ) //'18' is the distance the turret starts to look at you
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
if(curShoot)
{
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
} }
function Shoot(seconds) {
if ( Vector3.Distance( ememy.position, transform.position ) < 15 ) //'15' is the distance the turret starts to shoot at you { if(seconds!=savedTime) {
var bullit = Instantiate(bullitPrefab ,transform.Find("SpawnPoint").transform.position , Quaternion.identity); bullit.gameObject.tag = "ememyProjectile"; bullit.rigidbody.AddForce(transform.forward 1600);
bullit.rigidbody.AddForce(transform.up 45); savedTime=seconds; }
} }
Hope this helps
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Turret Range Script 2 Answers
Clarification on how RangeAttribute works 1 Answer
MouseButtonDown on button (Far away) NEED: Range for button 2 Answers
Detect if player is in range? 2 Answers