P
P
Pavel2018-09-09 23:08:33
Game development
Pavel, 2018-09-09 23:08:33

Why is one object moving faster than the others?

Here is the behavior.
The bottom line: I move one object, and broadcast the rest by how much and in what direction this object was shifted. However, the object that I move initially - moves faster than the rest.. I can not understand why.

//Объект который двигаю
void OnFixedUpdate()
    {
        if (IsDragged)
        {
            var lastPosition = transform.position;
            direction = cursorPosition - (Vector2)transform.position;
            transform.Translate(direction * Time.fixedDeltaTime * dragSpeed);
            if (OnDragEvent != null)
                OnDragEvent(this, transform.position - lastPosition);
        }
    }

//Список объектов повторяющих движение. Метод вызывается при генерации события OnDragEvent
void TranslateDrag(IDragable obj, Vector2 v)
    {
        for (int i = 0; i < groupObjects.Count; i++)
        {
            if (groupObjects[i] != obj)
                (groupObjects[i] as SelectableComponent).transform.position += (Vector3)v;
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel, 2018-09-10
@youkerni

if (groupObjects[i] != obj)
                (groupObjects[i] as SelectableComponent).transform.position += (Vector3)v;

This check was incorrect. I was comparing ISelectable and IDraggable, which caused the object I was moving to initially move twice by the same vector.

G
Griboks, 2018-09-09
@Griboks

You didn't normalize direction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question