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

Change position relative to object

Discussion in 'Scripting' started by smirlianos, Feb 5, 2015.

  1. smirlianos

    smirlianos

    Joined:
    Aug 2, 2012
    Posts:
    177
    Hello, I want some gameobjects to be alligned relative to a "leader" gameobject, so they can make a square. I store them in a list and change their position with an offset according to their list position. The problem is that the objects get alligned in world positions, not including the "leader" gameobject's rotation. Any help?

    Here's a drawing of what I mean:


    This is my script:

    Code (JavaScript):
    1. var posToGo : Vector3 = hit.point;
    2.                 for (var l : int = 0; l < man.selectedUnits.Count; l++)
    3.                 {
    4.                     if(man.selectedUnits.IndexOf(gameObject) == 0)
    5.                     {
    6.                         offX = 0;
    7.                         offZ = 0;
    8.                     }
    9.                     else if(man.selectedUnits.IndexOf(gameObject) == 1)
    10.                     {
    11.                         offX = 2;
    12.                         offZ = 0;
    13.                     }
    14.                     if(man.selectedUnits.IndexOf(gameObject) == 2)
    15.                     {
    16.                         offX = -2;
    17.                         offZ = 0;
    18.                     }
    19.                     else if(man.selectedUnits.IndexOf(gameObject) == 3)
    20.                     {
    21.                         offX = 0;
    22.                         offZ = -2;
    23.                     }
    24.                     if(man.selectedUnits.IndexOf(gameObject) == 4)
    25.                     {
    26.                         offX = 2;
    27.                         offZ = -2;
    28.                     }
    29.                     else if(man.selectedUnits.IndexOf(gameObject) == 5)
    30.                     {
    31.                         offX = -2;
    32.                         offZ = -2;
    33.                     }
    34.                     if(man.selectedUnits.IndexOf(gameObject) == 6)
    35.                     {
    36.                         offX = 0;
    37.                         offZ = -4;
    38.                     }
    39.                     else if(man.selectedUnits.IndexOf(gameObject) == 7)
    40.                     {
    41.                         offX = 2;
    42.                         offZ = -4;
    43.                     }
    44.                     if(man.selectedUnits.IndexOf(gameObject) == 8)
    45.                     {
    46.                         offX = -2;
    47.                         offZ = -4;
    48.                     }
    49.                 }
    50.                 posToGo.x += offX;
    51.                 posToGo.z += offZ;
    52.                 StartCoroutine(Motion(posToGo));
    Code (JavaScript):
    1. function Motion(pos : Vector3)
    2. {
    3.     //Debug.Log("Got inside");
    4.     while(Vector3.Distance(transform.position, pos) > 0.1)
    5.     {
    6.         animation.CrossFade("run");
    7.         var targetRotation = Quaternion.LookRotation(pos - transform.position);
    8.         transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10);
    9.         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movementSpeed);
    10.         if(Vector3.Distance(transform.position, pos) <= 0.1)
    11.         {
    12.             animation.CrossFade("idle");
    13.         }
    14.         yield;
    15.     }
    16. }
    17.  
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Why don't you put all "servants" as children of the leader ?

    They should follow automatically the leader.
     
  3. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    You can do as @Sbiz suggested, or you can rotate the objects' position around the leader's position using Transform.RotateAround.
     
  4. smirlianos

    smirlianos

    Joined:
    Aug 2, 2012
    Posts:
    177
    I don' want to make them children, because the "leader" may change from time to time, and I need to control each of them seperately.

    I need a way to offset the position taking in mind the rotation of the "leader"
     
  5. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    @shaderop Sbizz ! :eek: :p

    @smirlianos I don't see any problem to change the leader ? When the leader has to change, you just change the parent of each children...

    But if you don't want to do this, you can do as shaderop said ; using RotateAround.
     
  6. smirlianos

    smirlianos

    Joined:
    Aug 2, 2012
    Posts:
    177
    The... "servants"(!), are people that walk into the correct position. If I use RotateAround, they will have a strange walking path I believe. Is there a way to find the relative position of an object X units behind/side of it?
     
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You can have the direction :

    Code (CSharp):
    1. Vector3 heading = guy.transform.position - leader.transform.position;
    2. Vector3 direction = heading.normalized;
    And the distance between them with Vector3.Distance. So, if your little guys are well set when they pop, you can save all this data and when the leader moves, you just have to apply the same direction and distance. I guess.