|
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() { }
var seconds : int = Time.time; var oddeven = (seconds % 2); if(oddeven)
{
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 }
(comments are locked)
|
|
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.
(comments are locked)
|
|
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:
Letting Unity's collision system handle distances is preferable to checking the distance every frame (see Unity's Optimization Docs). Where should i add this script?
Feb 24 '10 at 09:07 PM
Whatee
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 ).
Feb 25 '10 at 02:09 PM
burnumd
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'.
Oct 29 '12 at 05:15 PM
bdover1284
(comments are locked)
|
|
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
var LookAtTarget:Transform; var damp = 1.7; var ememy : Transform; //Shooting var bullitPrefab:Transform; var savedTime=0; var curShoot = true; function Update ()
{
if(LookAtTarget)
{
Hope this helps
(comments are locked)
|
|
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.
Feb 24 '10 at 07:42 PM
runevision ♦♦
You can put it wherever works best for your script. Maybe where you select the LookAtTarget just do a check before assigning that value.
Feb 24 '10 at 08:30 PM
Shawn
(comments are locked)
|

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.