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 /
  • Help Room /
avatar image
0
Question by Dayner_Kurdi · Jun 20, 2016 at 03:50 PM · arraystransform.positionvector3.movetowards

anyway to ensure that A gameobject dose not choose to move to a position if it was occupied by another ?

I'm using an Array of gameobject to designate points in the 3D world, and then i want an NPC gameobject to move to one of these points at random after reaching one of them, my problem is that I do not want the gameobject to choose one of these points as it finale destination if it was occupied by another point.

 public class EnamyShipAI : MonoBehaviour {
 
     public float moveSpeedforward;
 
     GameObject[] movePointone = new GameObject[3];
 
     // Use this for initialization
     void Start () {
 
       movePointOne = GameObject.FindGameObjectsWithTag("MovePoint");
      
     }
     
     //Update is called once per frame
     void Update () {
 
         transform.position = Vector3.MoveTowards(transform.position,
                                   movePointOne[0].transform.position,
                                   moveSpeedforward * Time.deltaTime);
 
     }
 }


I was thinking of before the moving gameObject, it will choose one point at random, mark it as occupied so other Instantiated objects cannot choose it, then move to it, a point dose not go back choosable till the occupied object has moved.

but i'm not sure where to start to coding that idea.

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
1
Best Answer

Answer by Dibbie · Jun 20, 2016 at 04:03 PM

You could do an "occupied" idea.

In a separate script, you would just have 1 public variable called "occupied", essentially: public bool occupied;

Then, for example reasons, lets say you had called that script "PointChecker", so in your main script here, in update, you would do something like: //create a temp variable to store a random number based on the size of the "point" array int loc = Random.Range(0, movePointone.Length - 1);

 //Check if occupied
 if(movePointone[loc].GetComponent<PointChecker>().occupied == false){
 //move to location
 transform.position = Vector3.MoveTowards(transform.position,
                                    movePointOne[loc].transform.position,
                                    moveSpeedforward * Time.deltaTime);
 //Set this spot to "occupied"
 movePointone[loc].GetComponent<PointChecker>().occupied == true
 }

(C#, untested code)

Another way you could do it is checking if that spot has a child, so it would be something like: //create a temp variable to store a random number based on the size of the "point" array int loc = Random.Range(0, movePointone.Length - 1);

     //Check if occupied by parents, children
     if(movePointone[loc].transform.childCount == 0){
     //move to location
     transform.position = Vector3.MoveTowards(transform.position,
                                        movePointOne[loc].transform.position,
                                        moveSpeedforward * Time.deltaTime);
     //Set this spot to "occupied"
     transform.parent = movePointone[loc].transform;
     }

(C#, untested code)

Comment
Add comment · Show 1 · 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 Dayner_Kurdi · Jun 20, 2016 at 11:19 PM 0
Share

Thanks, that exactly what I wanted, i was going to trying to do it all in one single script, but your way is way more easier

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

57 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

Related Questions

Moving spawned enemies randomly towards several pre-defined positions 1 Answer

Random move,Random move and rotation 0 Answers

How to Spawn a Prefab at an already Spawned Prefab's that's from an array at their transform.position? 1 Answer

Vector3.MoveTowards and NavAgent weirdness 0 Answers

How can change camera local position smoothly between 3 point 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