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

Question Formation following

Discussion in 'Scripting' started by piggybank1974, Dec 29, 2021.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I've been trying to work out in my head a way to create another formation from a bunch of spheres I've placed "See screenshot", I thought I might have to use Cross/Product etc to do it but I'm not sure.

    So as you can see I've placed an example cube at 0,0,0.1 "sphere original 0,0,0" from the first sphere, and I wanted to place other cubes that have a distance of 0.1 or whatever from that but keeping with the original sphere formation but I just cannot work this out in my head, and some help would be appreciated.

    A - I did think of getting the angle from the first to second converting that to a vector.
    B - Taking the second position and offsetting that by 0.1
    C - Then taking that vector and * -1 or I think I could use vector3.reflect, to get an opposite side.

    is there a better way and if so an example would be great?
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    The dead-easiest way to maintain formation is to just child all of the objects to a common object that you move.
     
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Kurt-Dekker

    I should have mentioned that these spheres will not be there they are just used as positions, for a monster to follow, but I might want to move the monster to a different formation, hence the attempt of working out an offset formation from the original, but thanks for the reply.
     
  4. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    After a little more fiddling I'm closer "See Screenshot" but it follows the formation but goes outside, when it hits the top of the arc, it should stay inside.

    Code (CSharp):
    1.  
    2. public void BtnRun_Click()
    3.   {
    4.    Int32 mCount = mManagers.SceneManager.Path.transform.childCount;
    5.    for (int i = 0; i < mCount; i++)
    6.    {
    7.     Transform mTransform = mManagers.SceneManager.Path.transform.GetChild(i);
    8.  
    9.     float mAngle = Vector3.Angle(mTransform.position, Vector3.zero);
    10.  
    11.     Quaternion mRotation = Quaternion.AngleAxis(mAngle, Vector3.up);
    12.  
    13.     Vector3 mDirection = mRotation * transform.forward;
    14.  
    15.      GameObject mObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    16.     mObject.transform.localScale = new Vector3(0.05f, 0.05f, 0.05F);
    17.     mObject.transform.position = mTransform.position + (mDirection * 0.1F);
    18.    }
    19.   }
    20.  
     

    Attached Files:

  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I question your line 9 above. It gives you an angle between two vectors... and one of them is zero length. So I'm not even sure what it would return.
     
  6. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Kurt-Dekker

    The reason I choose to use Vector.zero, is that would give me the angle of the transform, but I needed to base it on something so I choose Vector.zero, then I converted that to a direction, which I think is already normalized.

    I'm just not sure how else to do it?
     
  7. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I finally got it working, so I thought I would post back here with the screenshot and the code.

    Firstly I would like to make it clear that the original Pathing was done by Sebastian Lague on YouTube, check out his Curve Editor/Path Editor videos, he is one of the best on the subject on Unity and it's easy viewing.

    I have made modifications to it to fit my needs in the past,
    Code (CSharp):
    1.  
    2. public void BtnRun_Click()
    3.   {
    4.    Int32 mCount = mManagers.SceneManager.Path.transform.childCount;
    5.    Vector3 mForward = Vector3.zero;
    6.    Vector3 mPosition = Vector3.zero;
    7.  
    8.    for (int i = 0; i < mCount; i++)
    9.    {
    10.     Transform mTransform = mManagers.SceneManager.Path.transform.GetChild(i);
    11.     mForward = Vector3.zero;
    12.  
    13.     if (i > 0)
    14.      mForward += mTransform.position - mManagers.SceneManager.Path.transform.GetChild((i - 1 + mCount) % mCount).position;
    15.     else
    16.      mForward = -(mTransform.right * 0.1F);
    17.  
    18.     mPosition = new Vector3(-mForward.z, 0F, mForward.x);
    19.  
    20.     // Left Side
    21.     CreateCube(mTransform.position, (mPosition + (mPosition.normalized * 0.1f)) );
    22.  
    23.     // Right Side
    24.     CreateCube(mTransform.position, ((mPosition + (mPosition.normalized * 0.1f)) * -1));
    25.    }
    26.   }
    27.  
    28.   private void CreateCube(Vector3 position, Vector3 offset)
    29.   {
    30.    GameObject mObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    31.    mObject.transform.localScale = new Vector3(0.05f, 0.05f, 0.05F);
    32.    mObject.transform.position = position + offset;
    33.   }
    34.  
    I'll provide the code here just in case anybody is interested, and I could create a unity package if one is needed down the line.

    there are several things this could be used for:

    A - Making enemies walk down different paths/lines, let's say what troops could do, e.g centre would be vehicles and the outers could be the troops.

    B - You could form a meshed path, with a little extra work you have all the endpoints "the cube positions" and creating the triangles/normals etc.
     

    Attached Files:

    Kurt-Dekker likes this.