Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by drdunson · Mar 18 at 10:28 PM · script error

The enemy ignores the targets. This is the code that I am using:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyMove : MonoBehaviour
 {
     private NavMeshAgent Nav;
     private Animator Anim;
     private Transform TheTarget;
     private float DistanceToTarget;
     private int TargetNumber = 1;
 
 
     [SerializeField] float StopDistance = 2.0f;
     [SerializeField] int MaxTargets = 16;
     [SerializeField] Transform Target1;
     [SerializeField] Transform Target2;
     [SerializeField] Transform Target3;
     [SerializeField] Transform Target4;
     [SerializeField] Transform Target5;
     [SerializeField] Transform Target6;
     [SerializeField] Transform Target7;
     [SerializeField] Transform Target8;
     [SerializeField] Transform Target9;
     [SerializeField] Transform Target10;
     [SerializeField] Transform Target11;
     [SerializeField] Transform Target12;
     [SerializeField] Transform Target13;
     [SerializeField] Transform Target14;
     [SerializeField] Transform Target15;
     [SerializeField] Transform Target16;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         Nav = GetComponent<NavMeshAgent>();
         Anim = GetComponent<Animator>();
         TheTarget = Target1;
     }
 
     // Update is called once per frame
     void Update()
     {
         DistanceToTarget = Vector3.Distance(TheTarget.position, transform.position);
         if(DistanceToTarget > StopDistance)
         {
             Nav.SetDestination(TheTarget.position);
         }
 
         if (DistanceToTarget < StopDistance)
         {
             TargetNumber++;
             if(TargetNumber > MaxTargets)
             {
                 TargetNumber = 1;
             }
             SetTarget();
         }
     }
 
     void SetTarget()
     {
         if(TargetNumber == 1)
         {
             TheTarget = Target1;
         }
         if (TargetNumber == 2)
         {
             TheTarget = Target2;
         }
         if (TargetNumber == 3)
         {
             TheTarget = Target3;
         }
         if (TargetNumber == 4)
         {
             TheTarget = Target4;
         }
         if (TargetNumber == 5)
         {
             TheTarget = Target5;
         }
         if (TargetNumber == 6)
         {
             TheTarget = Target6;
         }
         if (TargetNumber == 7)
         {
             TheTarget = Target7;
         }
         if (TargetNumber == 8)
         {
             TheTarget = Target8;
         }
         if (TargetNumber == 9)
         {
             TheTarget = Target9;
         }
         if (TargetNumber == 10)
         {
             TheTarget = Target10;
         }
         if (TargetNumber == 11)
         {
             TheTarget = Target11;
         }
         if (TargetNumber == 12)
         {
             TheTarget = Target12;
         }
         if (TargetNumber == 13)
         {
             TheTarget = Target13;
         }
         if (TargetNumber == 14)
         {
             TheTarget = Target14;
         }
         if (TargetNumber == 15)
         {
             TheTarget = Target15;
         }
         if (TargetNumber == 16)
         {
             TheTarget = Target16;
         }
     }
 }
 
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 andrew-lukasik · Mar 18 at 10:47 PM 0
Share

This code does the same but is much shorter and easier to understand imho:

 public class EnemyMove : MonoBehaviour
 {
     [SerializeField] NavMeshAgent navAgent = null;// fill this in the inspector
     [SerializeField] Animator animator = null;// fill this in the inspector
     [SerializeField] float stopDistance = 2.0f;// fill this in the inspector
     [SerializeField] Transform[] targets = null;// fill this in the inspector
     int targetIndex = 0;
     Transform target = null;
 
     void Start ()
     {
         target = targets[targetIndex];
     }
 
     void Update ()
     {
         float distance = Vector3.Distance( transform.position , target.position );
         if( distance>stopDistance )
         {
             navAgent.SetDestination( target.position );
         }
         else
         {
             targetIndex = (targetIndex+1)%(targets.Length-1);
             target = targets[targetIndex];
         }
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

180 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 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

A Problem With A Script Of GUI.HorizontalSlider It Do not Move? 0 Answers

I have a problem with this script and an error appeared, does anyone know why? 0 Answers

How to reference this game object to another class 0 Answers

Can someone tell me what is wrong with this script? 1 Answer

Im having a problem with my C# code 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