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 BritishGuy · Nov 08, 2014 at 12:52 AM · movemultiplenpcselectmany

Select and move 1 object, problem = moving multiple.

Hello

I am trying to setup a RTS type system where i can select a npc and move it around, It works fine when i have 1 npc in the scene to move, But if i duplicate that npc and select only 1 and move it, it will move all the npc's i did not select.

Q. What am i missing here? Thanks.

 private NavMeshAgent agent;
     private Transform constructor;
     private Animation anim;
     public bool selected;
     
     void Start () 
     {
         selected = false;
 
         agent = gameObject.GetComponent<NavMeshAgent> ();
         constructor = transform.FindChild("Constructor");
         anim = constructor.GetComponent<Animation>();
 
     }
 
     void Update()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         RaycastHit hit;
 
         if (Input.GetMouseButtonDown(0))
         {
 
             if (Physics.Raycast(ray, out hit))
             {
                 if(hit.transform.tag == "Player")
                 {
                     selected = true;
                     
                 }else{
                     
                     selected = false;
                 }
             }
         }
 
         if (Input.GetMouseButtonDown(1) && selected)
         {
             if (Physics.Raycast(ray, out hit))
             {
                 agent.SetDestination(hit.point);
             }
         }
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 FairGamesProductions · Nov 08, 2014 at 02:24 AM

I am assuming that you attach this script to the NPC? And the NPC has a tag called "Player"? If these 2 statements are true, then YES, when one of the NPCs is clicked, both "feel" it and act as if selected. The casting of the ray should be done in a separate script. Here is the script for the NPCs (JS):

 private var selected = false;
 private var agent : NavMeshAgent;
 private var anim : Animator;
 private var MoveTo : Vector3;
 private var NotSet = false;
 
 function Start ()
     {
     anim = gameObject.transform.FindChild("Constructor").GetComponent(Animator);
     agent = gameObject.GetComponent(NavMeshAgent);
     }
 
 function Update ()
     {
     if (selected && NotSet)
         {
         NotSet = false;
         agent.SetDestination(MoveTo);
         anim.SetTrigger("Move"); //I assume the NPC has a move animation
         }
     if (agent.pathStatus == NavMeshPathStatus.PathComplete)
         {
         anim.SetTrigger("Idle");
         }
     }
 
 function Select (x : int)
     {
     selected = true;
     }
 
 function Deselect (x : int)
     {
     selected = false;
     }
 
 function Destination (d : Vector3)
     {
     MoveTo = d;
     NotSet = true;
     }

And attached to another (unrelated) GameObject, like the Camera:

 private var selectedUnit : GameObject;
 
 function Update ()
     {
     var hit : RaycastHit;
     var ray: Ray = gameObject.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(ray, hit))
         {
         if (hit.transform.tag == "Player" && Input.GetMouseButtonDown(0))
             {
             selectedUnit = hit.transform.gameObject;
             selectedUnit.SendMessage("Select", 1);
             }
         if (hit.transform.tag == "Ground" && Input.GetMouseButtonDown(1))
             {
             selectedUnit.SendMessage("Destination", hit.point);
             }
         if (hit.transform.tag == "Ground" && Input.GetMouseButtonDown(0))
             {
             selectedUnit.SendMessage("Deselect", 1);
             }
         }
     }

To work on multiple units keep an array of the selected gameObjects, and use a for loop (example js):

 for (i in SelectedUnits)
         {
         i.SendMessage("Select", 1);
         }
Comment
Add comment · Show 6 · 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 BritishGuy · Nov 08, 2014 at 02:45 AM 0
Share

Thanks allot for the help, Been pulling my hair out over this. I am no good with js but i managed to convert it to c# incase anyone else stumbles onto this answer.

Thanks again.

 private bool selected = false;
     private Nav$$anonymous$$eshAgent agent;
     private Animator anim;
     private Vector3 $$anonymous$$oveTo;
     private bool $$anonymous$$ove = false;
     
     void  Start (){
         anim = gameObject.transform.FindChild("Constructor").GetComponent<Animator>();
         agent = gameObject.GetComponent<Nav$$anonymous$$eshAgent>();
     }
     
     void  Update (){
         if (selected && $$anonymous$$ove)
         {
             agent.SetDestination($$anonymous$$oveTo);
         }
         if (agent.pathStatus == Nav$$anonymous$$eshPathStatus.PathComplete)
         {
             $$anonymous$$ove = false;
         }
     }
     
     void  Select (int x){
         selected = true;
     }
     
     void  Deselect (int x){
         selected = false;
     }
     
     void  Destination (Vector3 d){
         $$anonymous$$oveTo = d;
         $$anonymous$$ove = true;
     }

and

 void  Update (){
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             if (hit.transform.tag == "Player" && Input.Get$$anonymous$$ouseButtonDown(0))
             {
                 selectedUnit = hit.transform.gameObject;
                 selectedUnit.Send$$anonymous$$essage("Select", 1);
             }
             if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(1))
             {
                 selectedUnit.Send$$anonymous$$essage("Destination", hit.point);
             }
             if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(0))
             {
                 selectedUnit.Send$$anonymous$$essage("Deselect", 1);
             }
         }
     }
avatar image FairGamesProductions · Nov 08, 2014 at 02:45 AM 0
Share

NP. Glad I could help :)

avatar image FairGamesProductions · Nov 08, 2014 at 02:47 AM 1
Share

just one thing, $$anonymous$$ake sure to set $$anonymous$$ove = false; in the first if. Otherwise agent.SetDestination($$anonymous$$oveTo); will happen every frame until it reaches the destination.

(See the revised code above)

avatar image FairGamesProductions · Nov 09, 2014 at 01:59 AM 1
Share

What I gave you was the start of the code you need. It obviously needs to be more complex for a full fledged strategy game. Just send a Deselect message to the currently selected until before sending the Select message to the new one (js):

 private var selectedUnit : GameObject;
  
  function Update ()
      {
      var hit : RaycastHit;
      var ray: Ray = gameObject.ScreenPointToRay(Input.mousePosition);
      if (Physics.Raycast(ray, hit))
          {
          if (hit.transform.tag == "Player" && Input.Get$$anonymous$$ouseButtonDown(0))
              {
              if (selectedUnit != null)
                  {
                  selectedUnit.Send$$anonymous$$essage("Deselect", 1); //This will deselect the previously selected unit (if any)
                  }
              selectedUnit = hit.transform.gameObject;
              selectedUnit.Send$$anonymous$$essage("Select", 1);
              }
          if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(1))
              {
              selectedUnit.Send$$anonymous$$essage("Destination", hit.point);
              }
          if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(0))
              {
              selectedUnit.Send$$anonymous$$essage("Deselect", 1);
              }
          }
      }
avatar image BritishGuy · Nov 09, 2014 at 03:47 PM 1
Share

Thanks again, I am in a learning phase at the moment, Nothing i do is for commercial use, Its more of a self taught hobby, What i tend to do is try and achieve something myself, if i get stuck get help, remember what i have been told then start again, try and replicate what i did remembering my faults. So thanks again much appreciated.

Show more comments

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

27 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

Related Questions

RPG style character with multiple items 1 Answer

How to add gameobject inside of an array? 0 Answers

more efficient = gameObject w. Material[5] or 5x GameObject w. material[1] 1 Answer

Reverting to prefab-state several game objects at once with stock Unity 3 Answers

Is it possible to add a script to multiple objects seleced in the inspector? 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