x


Turret script bug

I am attempting to make a turret that will only track and shoot at the player if it is with in a certain radius. I found a good script on this site, but when I use it, unity will freeze up when my character enters the trigger area.

The code I am using is this:

private var target : Transform;

var damping = 6.0;

var smooth = true;

var bulletPrefab : Rigidbody;

var bulletSpeed : float = 100;





function OnTriggerEnter(otherCollider : Collider) {

    if (otherCollider.CompareTag("player"))

    {

        target = otherCollider.transform;

        Fire();

    }

}



function OnTriggerExit(otherCollider : Collider) {

    if (otherCollider.CompareTag("player"))

    {

        target = null;

        StopCoroutine("Fire");  // aborts the currently running Fire() coroutine

    }

}



function Fire()

{

    while (target != null)

    {

        var nextFire = Time.time + 1;

        while (Time.time < nextFire)

        {



          if (smooth)

          {

              // Look at and dampen the rotation

              var rotation = Quaternion.LookRotation(target.position - transform.position);

              transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

          }

          else

          {

              // Just lookat

               transform.LookAt(target);

          }



        }



        // fire!

        var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);

        bullet.velocity = transform.forward * bulletSpeed;

    }

}

I have my fireball prefab linked to the bulletPrefab, my character is tagged "player", and I am using a spherical collider for the trigger. Any ideas why my game would crash?

more ▼

asked Oct 20 '11 at 12:39 AM

Tommas gravatar image

Tommas
1 3 3 3

Hmm... For some reason, it didn't like my while (target != null) loop. Reworking it to have an if statement, like in this reference http://unity3d.com/support/documentation/ScriptReference/Time-time.html fixed it. I still don't know why it crashed, but... it works!

Oct 20 '11 at 04:02 PM Tommas
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The while (target != null) loop has no way of exiting. It runs through the loop infinitely and crashes the program. I think what you're looking for is an IF statement rather than a WHILE statement. IF only iterates once and won't crash your program.,Your

more ▼

answered Oct 20 '11 at 05:35 PM

Gtaray 1 gravatar image

Gtaray 1
1 1 1 3

(comments are locked)
10|3000 characters needed characters left

Thommas - your functions "OnTriggerEnter" & "OnTriggerExit" will checked every frame and thus If player inside your Trigger then EVERY FRAME WILL EXECUTED "Fire"-but this function will executed when "while (target != null)"-it will forever in current frame. That's why you look mistake - when you insert two "if" without "while" your code will work right.

more ▼

answered Oct 20 '11 at 05:34 PM

alkor gravatar image

alkor
1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x984
x591
x172
x42

asked: Oct 20 '11 at 12:39 AM

Seen: 603 times

Last Updated: Oct 20 '11 at 05:35 PM