Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Nav Mesh Agent problem.

Discussion in 'Scripting' started by N1warhead, Jan 24, 2015.

  1. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Hey guys, I'm having problems with this Navmesh stuff.

    I have everything working, however, any transform this script is attached too,
    it makes all the gameObjects move to the same position on the map.

    I've tried every way I can think of.

    (This is for an RTS By the way.)


    Here is the code.

    Code (CSharp):
    1.     void SelectUnit(){
    2.         if (Input.GetButtonUp ("Fire1")) {
    3.             Debug.Log("Selected Unit");
    4.             RaycastHit hit;
    5.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    6.             if (Physics.Raycast (ray, out hit)){
    7.                 if(hit.transform.gameObject.tag == ("Player")){
    8.                     //this.gameObject.GetComponent<NavMeshAgent>();
    9.                     UnitSelected = true;
    10.                 }
    11.  
    12.             }
    13.             }
    14.         if (Input.GetButtonDown ("Fire2")) {
    15.             UnitSelected = false;
    16.  
    17.                 }
    18.         }
    19.  
    20.     void SingleUnitMovement(){
    21.         if(UnitSelected == true){
    22.             Debug.Log("Moving Unit");
    23.             RaycastHit hit;
    24.             if (Input.GetButtonDown ("Fire1")) {
    25.                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    26.                 if (Physics.Raycast (ray, out hit))
    27.  
    28.  
    29.                   // this.gameObject.GetComponent<NavMeshAgent>().SetDestination(hit.point);
    30.                
    31. this.navigationAgent.SetDestination (hit.point);
    32.             }
    33.         }
    34.                
    35.     }
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    So more than this GameObject move to this position?
     
  3. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I just kind of got it working.

    I did collider.Raycast ad now I can select specific units.

    However, if I want to go select a new unit by clicking on them, I am trying to cancel out the last selected unit so they
    don't all move again lol.