Search Unity

How can I add equal gap between the objects when they move and in the end ?

Discussion in 'Scripting' started by DubiDuboni, Sep 8, 2019.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    This make the objects move with equal gap between them since I'm setting the start time for each object to start after 2 seconds. But I want that even if the time to start will be for example 1 second and the gap is set to 2 then the objects will move while moving the gap between them will be 2.

    Also I want that the objects will end moving endPos with equal gaps.
    For example if there is 10 objects and if the first object moved to distance 2f the next object should move to 0.8f then 0.6f and so on or if I set the distance to move to 5 and there is 10 objects then 5/10 then the gap is 0.5f

    Or if I want the gap to be in the end 2 between each object.


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DropDown : MonoBehaviour
    7. {
    8.     public GameObject dropdownPrefab;
    9.     public int numberOfObjects;
    10.     public float speed = 1.5f;
    11.     public float duration = 5f;
    12.     public Vector3 startPos;
    13.     public Vector3 endPos;
    14.     public float distanceToMove = 2f;
    15.     public float gap;
    16.  
    17.     private List<GameObject> dropDownObjects = new List<GameObject>();
    18.  
    19.     private void Start()
    20.     {
    21.         for (int i = 0; i < numberOfObjects; i++)
    22.         {
    23.             dropDownObjects.Add(Instantiate(dropdownPrefab, transform.parent));
    24.         }
    25.  
    26.         //gap = distanceToMove / numberOfObjects;
    27.  
    28.         startPos = dropDownObjects[0].transform.position;
    29.         endPos = dropDownObjects[0].transform.position - Vector3.up * distanceToMove;
    30.     }
    31.  
    32.     private void OnMouseDown()
    33.     {
    34.         StartCoroutine(StartDropObjects());
    35.     }
    36.    
    37.     private IEnumerator StartDropObjects()
    38.     {
    39.         for (int i = 0; i < dropDownObjects.Count; i++)
    40.         {
    41.             // Random wait period before rotation starts
    42.             if (i == 0)
    43.             {
    44.                 //yield return new WaitForSeconds(0);
    45.             }
    46.             else
    47.             {
    48.                 //yield return new WaitForSeconds(Random.Range(0, 2f));
    49.             }
    50.  
    51.             yield return new WaitForSeconds(2f);
    52.  
    53.             StartCoroutine(Drop(dropDownObjects[i].transform, duration));
    54.         }
    55.     }
    56.  
    57.     private IEnumerator Drop(Transform objectToDrop, float duration)
    58.     {
    59.         float t = 0.0f;
    60.  
    61.         while (t < duration)
    62.         {
    63.             t += Time.deltaTime;
    64.  
    65.             objectToDrop.transform.position = Vector3.MoveTowards(objectToDrop.transform.position, endPos, speed * Time.deltaTime);
    66.  
    67.             yield return null;
    68.         }
    69.     }
    70. }
    71.  
    Here I tried to set the gap in the Inspector to 2 but still it's not setting it to 2 in the endPos and the objects are moving up instead down now :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DropDown : MonoBehaviour
    7. {
    8.    public GameObject dropdownPrefab;
    9.    public int numberOfObjects;
    10.    public float speed = 1.5f;
    11.    public float duration = 5f;
    12.    public Vector3 startPos;
    13.    public Vector3 endPos;
    14.    public float distanceToMove = 2f;
    15.    public float gap;
    16.  
    17.    private List<GameObject> dropDownObjects = new List<GameObject>();
    18.  
    19.    private void Start()
    20.    {
    21.        for (int i = 0; i < numberOfObjects; i++)
    22.        {
    23.            dropDownObjects.Add(Instantiate(dropdownPrefab, transform.parent));
    24.        }
    25.  
    26.        //gap = distanceToMove / numberOfObjects;
    27.  
    28.        startPos = dropDownObjects[0].transform.position;
    29.        endPos = dropDownObjects[0].transform.position - Vector3.up * distanceToMove;
    30.    }
    31.  
    32.    private void OnMouseDown()
    33.    {
    34.        StartCoroutine(StartDropObjects());
    35.    }
    36.  
    37.    private IEnumerator StartDropObjects()
    38.    {
    39.        for (int i = 0; i < dropDownObjects.Count; i++)
    40.        {
    41.            // Random wait period before rotation starts
    42.            if (i == 0)
    43.            {
    44.                //yield return new WaitForSeconds(0);
    45.            }
    46.            else
    47.            {
    48.                //yield return new WaitForSeconds(Random.Range(0, 2f));
    49.            }
    50.  
    51.            yield return new WaitForSeconds(2f);
    52.  
    53.            StartCoroutine(Drop(dropDownObjects[i].transform, duration));
    54.        }
    55.    }
    56.  
    57.    private IEnumerator Drop(Transform objectToDrop, float duration)
    58.    {
    59.        float t = 0.0f;
    60.  
    61.        while (t < duration)
    62.        {
    63.            t += Time.deltaTime;
    64.  
    65.            objectToDrop.transform.position = Vector3.MoveTowards(objectToDrop.transform.position, new Vector3(endPos.x, endPos.y * gap, endPos.z), speed * Time.deltaTime);
    66.  
    67.            yield return null;
    68.        }
    69.    }
    70. }
    71.