Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

squad control/object group management

Discussion in 'Navigation' started by Gamadrila, Dec 7, 2021.

  1. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    How to implement this? There is a group of objects on the stage. How to make that from the start point to the end point they reach the same formation? Can this be done via coordinate systems? For example, for a group, make a local coordinate system. Bind coordinates to each object in the group manually, through the inspector. When the mouse button was pressed, this group moved as one object. Each member of the group could bend around obstacles along its own trajectory. Through each other, they could pass.
     
  2. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    I taught them to walk in formation. Attached a script to each unit, and I attributed my own amendment to each unit.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class memberSquad1 : MonoBehaviour
    6. {
    7.          public Vector3 cell;
    8.    
    9. }
    10.  
    Another script moves units.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6.  
    7. public class Experement2 : MonoBehaviour
    8. {
    9.     private Camera cam;
    10.  
    11.      // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        cam = Camera.main;
    15.       }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         var sp = GetComponent<SelectionUnits11s>().rect;
    21.         List<GameObject> ts = GetComponent<SelectionUnits11s>().selectedObjects;
    22.  
    23.         if (
    24.             (Input.GetMouseButtonUp(0) & sp.size.x < 11)
    25.            
    26.             | (Input.GetMouseButtonUp(0) & sp.size.y < 11)
    27.             )
    28.         {
    29.  
    30.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    31.             if (Physics.Raycast(ray, out RaycastHit hit))
    32.             {
    33.                 if (hit.collider.gameObject.tag == "zemlya")
    34.                 {
    35.                     for (var st1 = 0; st1 < ts.Count; st1++)
    36.                     {
    37.  
    38.                         if (ts[st1].GetComponent<memberSquad1>())
    39.                         {
    40.                             var tsst1 = ts[st1].GetComponent<memberSquad1>().cell;
    41.                             tsst1 = tsst1 + hit.point;
    42.                             ts[st1].GetComponent<NavMeshAgent>().SetDestination(tsst1);
    43.                            
    44.                         }
    45.                         else
    46.                         {
    47.                             ts[st1].GetComponent<NavMeshAgent>().SetDestination(hit.point);
    48.                         }
    49.                     }
    50.                  
    51.                 }
    52.             }
    53.  
    54.         }
    55.     }
    56.  
    57.  
    58. }
    59.  
    But you need them to take their position relative to the angle of rotation. How to make such an amendment?
    How do you know the angle of rotation of a unit when it has finished its journey? But before the start of the movement.
     
  3. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    I drew a picture. Everything is known - the coordinates, the angle is rotated. I can not deploy objects relative to each other. How do I find the coordinates of two points?
     

    Attached Files:

    • 92.png
      92.png
      File size:
      18.6 KB
      Views:
      254
  4. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    I made a video that shows what I can't do. If you do not understand the question, please write.
     
  5. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    266
    To do that you need to capture the target formation, a circle, line, square, whatever as a collection of Vector3s. Set the Whenever a user clicks, you then choose points around that click that correspond to the target formation and set the destination of each agent to one of those.

    For example 4 units in a square, maybe you have:
    Code (CSharp):
    1. Vector3[] offsets = new Vector3[4];
    2. offsets[0] = new Vector3(-1, 0 -1);
    3. offsets[1] = new Vector3(-1, 0 1);
    4. offsets[2] = new Vector3(1, 0 -1);
    5. offsets[3] = new Vector3(1, 0 1);

    Whenever a user clicks to move the selected units, you set the destinations of each of the 4 units like:
    Code (CSharp):
    1. for(int i = 0; i < 4; i++)
    2. {
    3.     Agent[i].SetDestination(pointOnNavMesh + offsets[i]);
    4. }
     
    Gamadrila likes this.
  6. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    I have not tested your code. But I think you wrote the same thing that I wrote in the second post.
    Your "offsets", this is my "cell".
    Your "pointOnNavMesh + offsets ", this is my "tsst1 + hit.point ".
    But I have already solved the problem.


    Code (CSharp):
    1.  Vector3 relativePos = hit.point - Unit2.transform.position;  // The vector from the center object to the raycast point.
    2. var d = Quaternion.LookRotation(relativePos); //angle of rotation
    3. Vector3 pryamo = Quaternion.AngleAxis(d.eulerAngles.y, Vector3.up) * Vector3.forward; //direction vector
    4. Vector3 pa1 = Quaternion.AngleAxis(-90, Vector3.up) * pryamo;  // the vector of the direction of the raycast point to the cell, the angle at -90 is known, since the cells are static
    5.  Vector3 a1 = pa1 * 80 + hit.point;  // distance 80 between cells is known in advance
    So for every point.
    These four lines of code took me a month. Now I am finalizing the script. Made management without a central facility. Now I am doing the management of the detachment units.
     
    Last edited: Dec 31, 2021