Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ProductDelta · Aug 13, 2017 at 06:19 AM · unity 5script.

Help adjusting my code

I have the following code in order to make my turret face the nearest target and I cant get it to work. Any suggestions?

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq;

public class Turret : MonoBehaviour { GameObject[] enemies; GameObject player; GameObject closestEnemy; private Vector3 target; int count = 0;

 private void Update()
 {
     InvokeRepeating("FindNearestEnemy", 0f, 0.5f);
     target = closestEnemy.transform.position;
 }

 GameObject FindNearestEnemy()
 {
     float minDistance = Mathf.Infinity;

     enemies = GameObject.FindGameObjectsWithTag("Enemy");
     foreach (GameObject enemyItem in enemies)
     {
         float dist = Vector3.Distance(player.transform.position, enemyItem.transform.position);

         if (count == 0)
         {
             minDistance = dist;
             closestEnemy = enemyItem;
             count++;
         }
         else
         {
             if (dist < minDistance)
             {
                 minDistance = dist;
                 closestEnemy = enemyItem;

             }
         }
     }
     return closestEnemy;

 }
  
 

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Aug 13, 2017 at 06:31 AM

Don't call InvokeRepeating in Update - you'll end up queuing an infinite number of method calls and crash your game very quickly. If tou only want to retarget every half second:

 void Start(){
   InvokeRepeating("FindNearestEnemy", 0f, 0.5f);
 }
Comment
Add comment · Show 11 · 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 ProductDelta · Aug 13, 2017 at 06:32 AM 0
Share

Even in Start it doesn't track and do what it is supposed to do

avatar image ProductDelta · Aug 13, 2017 at 06:32 AM 0
Share

I get NullReferenceException: Object reference not set to an instance of an object Turret.FindNearestEnemy () (at Assets/Scripts/Turret.cs:28)

avatar image tanoshimi ProductDelta · Aug 13, 2017 at 06:36 AM 0
Share

Give us a clue.... what line does that refer to?

avatar image ProductDelta tanoshimi · Aug 13, 2017 at 06:40 AM 0
Share
         float dist = Vector3.Distance(player.transform.position, enemyItem.transform.position);
Show more comments
avatar image
0

Answer by Komayo · Aug 13, 2017 at 07:33 AM

Also after find the nearest target, dont forget to setup the facing of the player/turret to him.

Comment
Add comment · Show 3 · 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 ProductDelta · Aug 13, 2017 at 07:35 AM 0
Share

thanks so much you interested in reward points?

avatar image Komayo ProductDelta · Aug 13, 2017 at 05:41 PM 0
Share

Yes please @ProductDelta Thank you.

avatar image ProductDelta Komayo · Aug 13, 2017 at 06:46 PM 0
Share

I had about 6 yesterday but then when I tried to give you some I only had 1, but whatever

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

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My player keeps sliding after I use my dash, help! 0 Answers

Hello everyone, I would like to create a Geometry dash like triangle Transporter in unity. 0 Answers

Play MovieTexture Backwards 1 Answer

How would this EventTrigger look in c# 1 Answer

HELP! Scripts looks fine but doesn't respond? 1 Answer


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