- Home /
Question by
JazzToTheTwo · Aug 28, 2021 at 09:38 AM ·
mousepositiondelta
Making one object copy the movements of another when dragged by the mouse.,Moving object in relation to mouse
I have two objects in the scene. One that I can click on and move, the other that i want to copy the movements of the first one.
So in one script i use this. Leaving out most of the code, just need the bool isDrag
unity UnityEngine.EventSystems;
public class DragDrop : monoBehavior ...
Public bool isDrag;
public void OnBeginDrag(PointerEventData eventData){
isDrag = true;
}
public void OnEndDrag(PointerEventData eventData){
isDrag = false;
}
Then in the next script i want ObjectB to detect when isDrag == true and then follow the mouse movements, but not to jump to the mouse rather keep the offset it has?
This is something that ive tried but it didn't work out
public void Update(PointerEventData eventData) {
if (isDrag == true){
rectTransform.anchoredPosition += eventData.delta;
}
}
Now that didn't work because you cant feed Update (PointerEventData eventData) but I'm just not sure what to do. I'm very new to coding and unfortunately i just don't know what i don't know. Ive tried a decent amount of ideas i had to get this to work but i cant figure it out. Any help is super appreciated
Comment