S
S
Slavon72021-08-11 18:05:36
C++ / C#
Slavon7, 2021-08-11 18:05:36

How to move many objects from A to point B of a unit?

How to move many objects from A to point B unity.
I have 30 objects that I drag and drop to different positions, for each object I have a start_point and an end_point. Thus I move these objects. But the problem is that it's very clumsy to assign each object a start and end point. The objects have to be around the starting position. How can the code and implementation be simplified? I'm sure that objects can be put in an array, but how can I give each object a different position? It is important that all objects become around

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fraa-Erasmas, 2021-08-12
@Slavon7

If I correctly understood the essence of the question, then it can be done very simply. Create an array with objects ObjectMovement[] objectMovementsand find circle points with a given radius.

float newX = startPosition.x + radius * Mathf.Cos(angleInUnit * i * Mathf.Deg2Rad);
float newY = startPosition.y + radius * Mathf.Sin(angleInUnit * i * Mathf.Deg2Rad);
After transferring the new position to the object on the stage.
// Повесить скрипт на объект, который нужно переместить

/// <summary>
/// Объект который нужно переместить
/// </summary>
public class ObjectMovement : MonoBehaviour {
    /// <summary>
    /// Изменить расположение объекта
    /// </summary>
    /// <param name="vector"> новая позиция </param>
    public void ChangePosition(Vector2 vector) {
        transform.position = vector;
    }
}

/// <summary>
/// Менеджер объектов
/// </summary>
public class ObjectsMovementManager : MonoBehaviour {
    /// <summary>
    /// Список нужных объектов
    /// </summary>
    public ObjectMovement[] objectMovements = null;

    /// <summary>
    /// Стартовая позиция объектов
    /// </summary>
    public Vector2 startPosition = new Vector2(0, 0);

    /// <summary>
    /// Радиус окружности
    /// </summary>
    [Range(0.1f, 10.0f)] public float radius = 5;

    private void Update() {
        // Если кнопка нажата, то выполнить действие ниже
        if (Input.GetKeyDown(KeyCode.L)) {
            // Смещение в градусах от кол-ва объектов
            float angleInUnit = 360 / objectMovements.Length;
            
            // Пробегаемся по всем объектам в списке
            for (int i = 0; i < objectMovements.Length; i++) {
                // Считаем новую позицию по оси X
                float newX = startPosition.x + radius * Mathf.Cos(angleInUnit * i * Mathf.Deg2Rad);
                // Считаем новую позицию по оси Y
                float newY = startPosition.y + radius * Mathf.Sin(angleInUnit * i * Mathf.Deg2Rad);
                // Присваиваем объекту новую позицию
                objectMovements[i].ChangePosition(new Vector2(newX, newY));
            }
        }
    }
}

Before:
611525903c404230272741.png
After:
611525e85b5bf960679803.png
PS My solution is far from the most efficient, but that's all I could think of)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question