Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Sibic · Aug 14, 2015 at 04:14 PM · aiquaternionnavmeshagentlooppatrol

Need help with multiple patrols on a AI, looping.

So i figured out how to switch from a randomized navamesh patrol to a flying patrol. But i haven't been able to reset it and make it into a constant loop, anyone have any advice how to accomplish this? Also it says that "Array index is out of range" when it gets to the end of the all the currentpatrolpoints. It says this line is causing it,Quaternion rotation = Quaternion.LookRotation(Patrolpoints[CurrentPatrolpoint].transform.position - transform.position);

using UnityEngine; using System.Collections;

public class MosquitoAI : MonoBehaviour { public GameObject[] Patrolpoints; public GameObject[] waypoints; public Transform player;

 NavMeshAgent NavMeshA;

 public int MaxPatrolpoints;

 public float Timer = 500;
 
 public float Distance;
 public float Turnamount;
 public float Forwardamount;
 public float Speed;
 public float TravelSpeed;
 public float Damping;
 public float TimeDamper;

 public int CurrentWaypoint;
 public int CurrentPatrolpoint;

 public bool flymode;
 public bool Looper;
 
 void Awake()
 {
     NavMeshA = GetComponent<NavMeshAgent>();
     NavMeshA.updatePosition = true;
     NavMeshA.updateRotation = true;
     
     waypoints = GameObject.FindGameObjectsWithTag("WaypointMosquito");
     CurrentWaypoint = Random.Range (0, waypoints.Length); 

     Looper = false;
 }

 void Update()
 {
     Timer--;

     if(Timer <= 0)
     {
         flymode = true;
     }
     else
     {
         Ground();
         NavMeshA.enabled = true;
     }
     if (flymode == true) 
     {

         NavMeshA.enabled = false;
         Flying();
     }
 }

 void Ground()
 {
     NavMeshA.speed = Speed;
     
     if(Vector3.Distance(this.transform.position, waypoints[CurrentWaypoint].transform.position) >= 2)
     {
         NavMeshA.SetDestination(waypoints[CurrentWaypoint].transform.position);
         MovementManager(Vector3.zero);
     }
     else if(Vector3.Distance(this.transform.position, waypoints[CurrentWaypoint].transform.position) <= 2)
     {
         CurrentWaypoint = Random.Range (0, waypoints.Length);
     }
     else
     {
         MovementManager(Vector3.zero);
     }
 }

 void Flying()
 { 

     if(Vector3.Distance(this.transform.position, Patrolpoints[CurrentPatrolpoint].transform.position) <=2)
     {
         CurrentPatrolpoint ++;
     }
     if(CurrentPatrolpoint >= Patrolpoints.Length) 
     {
             Debug.Log ("working");
             flymode = false;
             NavMeshA.enabled = true;
             Ground();
     }

     Quaternion rotation = Quaternion.LookRotation(Patrolpoints[CurrentPatrolpoint].transform.position - transform.position);
     transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * TimeDamper);
     
     transform.Translate (Vector3.forward * TravelSpeed * Time.deltaTime);
     MovementManager(Vector3.zero);
 }
 
 void MovementManager(Vector3 move)
 {
     if(move.magnitude < 1f)move.Normalize();
     move = transform.InverseTransformDirection(move);
     Turnamount = Mathf.Atan2 (move.x, move.z);
     Forwardamount = move.z;
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by Lahzar · Aug 18, 2015 at 01:31 PM

You dont reset the currentpatrolpoint properly. On line 60 you need to add something like:

 if(currentPatrolPoint >= patrolPoints.Length-1) {
     currentPatrolPoint = 0;
 } else if(currentPatrolPoint < 0) {
     currentPatrolPoint = patrolPoints.Length-1
 }
     
     //Set waypoint

Remember that array.Length will return the amount of things in the array, not the index. So array[array.Length] doesn't exist. That is usually why people get that error.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Patrol system not working? Ignoring points(?) 0 Answers

AI will complete patrolling and wait for 5 seconds 2 Answers

Find the nearest location and move to a target outside Navigation Mesh? 1 Answer

Way to find closest point in a specific area on navmesh. 0 Answers

Having trouble with enemy AI and Collider 0 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