- Home /
Why the object being moved is faster than the rest?
Behaviour i have now - http://skrinshoter.ru/v/090918/lZLCoJYj?a
I move one object, for other I translate by how much and in what direction this object was moved. However, the object that I move initially - moves faster than others .. I can not understand why.
//This object moved by player
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);
}
}
//This method repeat the movement on another objects. Method called when OnDragEvent executed
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;
}
}
I'm not sure, but...
HAve the objects the same scale? Depending on how you are moving them, is possiblee that scale affects the realworld distance to move.
Check it. Bye!ª
Are they the same or are they 1,1,1? Do you have a parent object for those objects? Your code assumes a scale of 1,1,1 for the parent since you move your dragged object in local space while you move the "other" objects in worldspace.
Translate without a Space parameter defaults to localspace. Setting ".position" of an object moves it in worldspace. So you should either use .position in both cases, or use Translate in both cases. $$anonymous$$ixing them is not a good idea.
Answer by Eno-Khaon · Sep 10, 2018 at 09:13 AM
I don't see any sanity checks in place by your example to prevent the TranslateDrag() event from affecting the object you're actively moving as well.
I would venture a guess that when you drag one object around, you move it, then you tell all of the objects, including the one you just moved, to move by the same amount.
As a result, the object you drag is moving exactly twice as fast as all of the others linked to it.
What you may want to do, then, is not move the object during your isDragged check in OnFixedUpdate(). Instead, simply record the value for the movement without moving the object and let the OnDragEvent handle the movement of the selected object, too.
Right. We don't even know what GroupObjects actually are. Are they parent objects for all those nodes? If that are the actual objects, what is the exact element type of the groupObjects list? Since "this" is passed as obj parameter it would be important that the same component is actually inside that list.
I also would strongly recommend to change the as cast into a normal cast. An as cast without null check is never a good idea.
Finally we can only speculate where the code is actually called from. It seems it's called inside FixedUpdate. Though i don't see any reason why you want to do this. Any sort of input is processed per visual frame. The process of dragging an object is a visual thing which is directly linked to the input. So you should do this in Update. Also you can always use Time.deltaTime. When deltaTime is read from inside a FixedUpdate callback it will return the value of fixedDeltaTime. So you don't have to care about from where a certain piece of code is called when you use Time.deltaTime.