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 Jasper1900 · Oct 10, 2013 at 07:34 PM · airandompathfindingwaypoint

AI Pathfinding

Hi

I wrote this code today, but it doesn't work the way I want it to work. I want a object to move to one out of four points, and if reached go the another, random one.

The only problem is that if I change the code, it will search a new target every frame, and otherwise the code won't even search a target.

I tried it with to fix it with some booleans etc. but nothing seems to work..

Does anybody know how to fix this code?

The code so far:

 using UnityEngine;
 using System.Collections;
 using Pathfinding;
 
 public class Citizen_AI : MonoBehaviour {
    
     public GameObject targetObject;
     public GameObject targetObject1;
     public GameObject targetObject2;
     public GameObject targetObject3;
     public GameObject targetObject4;
     [HideInInspector]
     public Vector3 targetPosition;
     public float objectNumber;
     public bool isPicked = false;
     private Seeker seeker;
     private CharacterController controller;
  
     //The calculated path
     public Path path;
     
     //The AI's speed per second
     public float speed = 100;
     
     //The max distance from the AI to a waypoint for it to continue to the next waypoint
     public float nextWaypointDistance = 3;
  
     //The waypoint we are currently moving towards
     private int currentWaypoint = 0;
  
     public void Start () {
         seeker = GetComponent<Seeker>();
         controller = GetComponent<CharacterController>();
         //targetPosition = targetObject.transform.position;
         objectNumber = Random.Range(0, 4);
         Debug.Log("Picked New Number!");
         isPicked = true;
         if(objectNumber == 1){
                 targetObject = targetObject1;
         }
         if(objectNumber == 2){
                 targetObject = targetObject2;
         } 
         if(objectNumber == 3){
                 targetObject = targetObject3;
         }
         if(objectNumber == 4){
                 targetObject = targetObject4;
         } 
         targetPosition = targetObject.transform.position;
         //Start a new path to the targetPosition, return the result to the OnPathComplete function
         seeker.StartPath (transform.position,targetPosition, OnPathComplete);
     }
     
     public void OnPathComplete (Path p) {
         Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
         if (!p.error) {
             path = p;
             //Reset the waypoint counter
             currentWaypoint = 0;
         }
     }
  
     public void Update () {
         if (path == null) {
             //We have no path to move after yet
             return;
         }
         
         if (currentWaypoint >= path.vectorPath.Count) {
             isPicked = false;
             if(isPicked = false){
             PickRandomNumber();
             isPicked = true;
             }
             return;
         }
         
         //Direction to the next waypoint
         Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
         dir *= speed * Time.fixedDeltaTime;
         controller.SimpleMove (dir);
         
         //Check if we are close enough to the next waypoint
         //If we are, proceed to follow the next waypoint
         if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
             currentWaypoint++;
             return;
         }
     }
     
      public void PickRandomNumber () {
         objectNumber = Random.Range(0, 3);
         //while (  objectNumber != objectNumber ) objectNumber = Random.Range(0, 3);
         Debug.Log("Picked New Number!");
             if(objectNumber == 0){
                 targetObject = targetObject1;
         }
             if(objectNumber == 1){
                 targetObject = targetObject2;
         } 
             if(objectNumber == 2){
                 targetObject = targetObject3;
         }
             if(objectNumber == 3){
                 targetObject = targetObject4;
         } 
         targetPosition = targetObject.transform.position;
         //Start a new path to the targetPosition, return the result to the OnPathComplete function
         seeker.StartPath (transform.position,targetPosition, OnPathComplete);
     }
 }
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
3
Best Answer

Answer by clunk47 · Oct 10, 2013 at 07:41 PM

 //Example.CS
 
 //Example.CS
     
 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     GameObject[] points;
     int index;
     float speed = 5.0f;
     
     void Awake()
     {
         //Assuming each point has Tag "WayPoint".
         points = GameObject.FindGameObjectsWithTag("WayPoint");
         index = Random.Range (0, points.Length - 1);
         print (points.Length);
     }
     
     void Update()
     {
         transform.position = Vector3.MoveTowards(transform.position, points[index].transform.position, speed * Time.deltaTime);
         if(transform.position.Equals(points[index].transform.position))
             index = Random.Range(0, points.Length);
     }
 }
     
 
 
 
 
Comment
Add comment · Show 2 · 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 vexe · Oct 28, 2013 at 09:34 PM 1
Share

No need to actually subtract one in Random.Range (0, points.Length - 1) as the int version of Random.Range is exclusive at the end [$$anonymous$$IN, $$anonymous$$AX[

avatar image clunk47 · Oct 28, 2013 at 09:47 PM 0
Share

Ahh totally missed that when I did this man. $$anonymous$$AX will always be One less than what you define. So max in the case of points.Length being 10 for example, would be 9, which works with indexes starting with Zero. So yeah, if I subtract 1 it would be a max of 8, which wouldn't work right. Good pointing that out, Vece. Will edit answer.

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

18 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

Related Questions

How can I make AI follow waypoints and choose random ones? 1 Answer

How can I make an item choose between several different waypoints randomly and then make it execute something when it arrives to the waypoint? 3 Answers

Wandering enemies 1 Answer

Incredibly high speed tweening/enemy movement 0 Answers

Enemy detection while pathfinding through the map 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