Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tommas · Oct 20, 2011 at 12:39 AM · triggercrashturrettracking

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tommas · Oct 20, 2011 at 04:02 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Gtaray 1 · Oct 20, 2011 at 05:35 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by alkor · Oct 20, 2011 at 05:34 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

multipe targets 1 Answer

Enemy not detecting player 1 Answer

target tracking with lead time 1 Answer

Least heavy processing detection method 1 Answer

Glitchy Turret Script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges