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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Hobbyist Homeworld Movement system

Discussion in 'Scripting' started by buckius82, May 12, 2015.

  1. buckius82

    buckius82

    Joined:
    Nov 19, 2013
    Posts:
    40
    Currently as a hobbyist i have been learning programming while working on projects for games that i'd like to play some day. when i want to learn something i look at 4 different projects i am work on and see which is best suited.
    example: How do i do Homeworld 2 movement? so i figured it out while attempting a starfleet battles style 3d space strategy game.

    The solution i came up with use a nav beacon: if a unit or units are selected and the user presses the 'M' key
    instance a new beacon(the visual representation of were the unit will move)
    the nav beacon contains a line render-er that renders a line to the unit from the beacon. in the case of multiple units it uses an average all units positions just add all the units vector3 positions than divide by the number of units.

    these are the Variables

    Code (CSharp):
    1.  public Player player; // contains the selected objects list
    2.     private bool MoveMode = false; // enables movement
    3.     private bool Verticle = false; // toggles between vertical and horizontal movement
    4.     public GameObject beacon;      // prefab that contains the line renders a visual represntation of the system
    5.     public GameObject currentbeacon; //  the instanced navbeacon
    6.     private LineRenderer horizLinerenderer; // horizantal line render that goes from the nav beacon to the selected unit
    7.     private LineRenderer vertLinerenderer;    // vertical line render that goes from the nav beacon up or down in line with the unit
    8.     private float xinput; // used modify the nav beacons x pos                    
    9.     private float zinput; // used modify the nav beacons x pos
    10.     private float yinput; // used modify the nav beacons x pos
    11.     private float Damp = 0.1f;  // used modify the nav beacons x,y,z pos
    the update function

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         mask = (LayerMask)cam.cullingMask; // used for selection
    4.         if(!MoveMode){
    5.             MouseActivity();  // used for selection
    6.         }
    7.  
    8.         if(Input.GetKeyUp(KeyCode.M) && player.SelectedObject != null){
    9.             MoveMode = !MoveMode;                     // toggles the move mode
    10.             Cursor.visible = MoveMode;                // hides the cursor
    11.             EnterMoveMode(MoveMode);                  // this function instances the nev beacon and initializes the variables, when false destroys the beacon
    12.         }
    13.         if(Input.GetKeyUp(KeyCode.LeftShift) && MoveMode){
    14.             Verticle = !Verticle; // toggles vertical move
    15.         }
    16.     }
    the enter movemode function

    Code (CSharp):
    1. private void EnterMoveMode(bool enter){
    2.         if(enter && beacon){
    3.             GameObject go = GameObject.Instantiate(beacon, this.transform.position, Quaternion.identity) as GameObject;
    4.             currentbeacon = go;
    5.             go.SetActive(true);
    6.             horizLinerenderer = go.GetComponent<LineRenderer>();
    7.             vertLinerenderer = go.transform.GetChild(0).GetComponent<LineRenderer>();
    8.  
    9.             currentbeacon.transform.position = player.SelectedObject.transform.position;
    10.         }
    11.         if(!enter){
    12.             Verticle = false;
    13.             Destroy(currentbeacon);
    14.         }
    15.     }
    to update the linerenderers

    Code (CSharp):
    1. private void upDateLineRenderers(){
    2.         horizLinerenderer.SetPosition(0, currentbeacon.transform.position);
    3.         horizLinerenderer.SetPosition(1, player.SelectedObject.transform.position);
    4.  
    5.         vertLinerenderer.SetPosition(0,currentbeacon.transform.position);
    6.         vertLinerenderer.SetPosition(1,new Vector3(currentbeacon.transform.position.x,player.SelectedObject.transform.position.z,currentbeacon.transform.position.z));
    7.     }
    the fixed update function

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         if(MoveMode && !Verticle)
    4.         {
    5.             xinput = Input.GetAxis("Mouse X")*Damp;
    6.             zinput = Input.GetAxis("Mouse Y")*Damp;
    7.             yinput = player.SelectedObject.transform.position.y;
    8.             Vector3 newv3 = new Vector3(xinput,yinput,zinput);
    9.             if(currentbeacon != null){
    10.                 currentbeacon.transform.position += newv3;
    11.             }
    12.         }
    13.         if(MoveMode && Verticle)
    14.         {
    15.             xinput = player.SelectedObject.transform.position.y;
    16.             zinput = player.SelectedObject.transform.position.z;
    17.             yinput = Input.GetAxis("Mouse Y")*Damp;
    18.        
    19.             Vector3 newv3 = new Vector3(xinput,yinput,zinput);
    20.             if(currentbeacon != null){
    21.                 currentbeacon.transform.position += newv3;
    22.             }  
    23.         }
    24.         if(MoveMode && currentbeacon){
    25.             upDateLineRenderers();
    26.         }
    27.     }
    the final part to be be added is the actual move order. currently if the object is far away it aligns then "warps" to it else it just moves forward and turns to it.

    just thought i'd share.
    one problem i did run into is i have not figured how to create the circles that are in the homeworld UI. Any Suggestions?
    i was thinking about the Maybe Using the 4.6 UI sprites in some way.
     
    theANMATOR2b likes this.
  2. rboerdijk

    rboerdijk

    Joined:
    Aug 4, 2018
    Posts:
    96
    Old post, but since I coincidentally implemented exactly this last week and since someone else might run into this question, answering it anyway. I implemented it using vectrosity (not affiliated, it just happened to be part of a Unity bundle earlier this month). The documentation is quite good, you use it's linerenderer i contiguous mode and only have to generate some points on the circle. Advantage is that the line has the same thickness, irrelevant of how far you zoom in.

    You could also do it using standard unity linerenderer, free obviously, but the looks depends on how far you zoom in/out (looks thicker/thinner depending on zoom).
     
  3. buckius82

    buckius82

    Joined:
    Nov 19, 2013
    Posts:
    40
    thats a great idea totally forgot about this post lol

    I was messing with a new script a few weeks ago one thing i wanted to do was make the new move to points navigatin orders with rotation when i update it i'll post it
     
    marcusgamedesigner likes this.
  4. marcusgamedesigner

    marcusgamedesigner

    Joined:
    Jul 6, 2019
    Posts:
    1
    any luck? I've been trying to do the homeworld style of movement myself but I'm more of a designer than a coder so I'm struggling
     
  5. buckius82

    buckius82

    Joined:
    Nov 19, 2013
    Posts:
    40
    I did add Nav points by Holding shift unfortunately the original were corrupted had to start over,

    hardest thing i ran into is the UI systems

    Once i find and clean up the code i'll post it