- Home /
Start dragging an UI Image and then keep dragging a 2d collider
Hey everyone. This is what I want to achieve:
GOAL
Player start dragging an image from the UI (a BUILDING UI in the shop)
As soon as player exits from the UI I close the UI and I would like to spawn a new gameobject with a 2d collider on it (I'm calling I'll call this a BUILDING GAMEOBJECT)
After the spawn I would like to keep dragging the BUILDING GAMEOBJECT
I want to use the IDragHandler interface and both the dragging are working.
WHAT I CAN DO
I can drag the BUILDING UI on the UI and I can drag the BUILDING GAMEOBJECT on the gameboard using IDragHandler interface .
At the moment I'm also able to spawn the BUILDING GAMEOBJECT right in the place as soon as player exit from the UI.
WHAT I AM MISSING
When player exit from the UI the BUILDING GAMEOBJECT will be placed in the right place. But player will need to click on it again to start dragging it.
So at the moment player needs to do one other step
player clicks on BUILDING UI and start draggin
As soon as player exit from the UI I close the UI and I spawn the BUILDING GAMEOBJECT under mouse pointer
NON DESIRED Player needs to click on BUILDING GAMEOBJECT to start dragging it
Player can drag the BUILDING GAMEOBJECT on the board
QUESTION
Is there any way to keep dragging the BUILDING GAMEOBJECTand remove STEP 3 using IDragHandler interface ?
BUILDING UI CODE
public class ShopBuildingUI : MonoBehaviour, IDragHandler
{
//this event is used to check if player has moved the item around
public void OnDrag(PointerEventData eventData)
{
//if we're still on the ui we just move the image
MoveImage(eventData);
}
//moving the image around
private void MoveImage(PointerEventData eventData)
{
//move the image
_itemImage.SetPosition(eventData.delta);
//check if we're moving out of the ui
if (eventData.pointerCurrentRaycast.gameObject.tag == "UserInterface") return;
//pass the control to the building gameobject
MovedOnGameboard(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
//this happens if the building has been moved on the gameboard
private void MovedOnGameboard(Vector3 worldPosition)
{
//DISABLE THIS UI ELEMENT
...
//spawn a new building on the spot
_building.SpawnView(worldPosition);
}
BUILDING GAMEOBJECT CODE
public class Building : MonoBehaviour, IDragHandler
{
//dragging move the item in the world
public void OnDrag(PointerEventData eventData)
{ transform.position = eventData.pointerCurrentRaycast.worldPosition; }
}
probably you are using On$$anonymous$$ouseDrag() or OnPointerDown() or similar events for start dragging this Building Object, all you need to do in cut all code inside them and paste them to another method. than in your UIBuildingDrag script, you should fire an event right after you instantiated buildingObject for calling that drag method.
I suggest you to check this also you can look C# events too.
if you need further assistance or if have a hard time to understand this answer, just comment below.
I hope this answer helps, please inform us your results.
I added my code above. I'm not using any starting method. I just get the OnDrag handler of iDragHandler on both elements.
I can't trigger it one time with a UnityEvent. I would need it to keep triggering until player doesn't stop dragging.
I could move everything out of the OnDrag and just keep dragging until I get a basic Input.Get$$anonymous$$ouseUp or something like that. But I'd like to know if I can keep using the OnDrag handler.
Edit: just a further note. The BUILDING GA$$anonymous$$EOBJECT catches the OnDrag with a 2d collider and a 2dPhysicsRaycaster on the main camera.
I did not test it but something like this also could help too :
bool isJustSpawned =true;
RaycastHit hit;
private void Start() {
$$anonymous$$oveBuildingObject();
}
public void OnDrag(PointerEventData eventData)
{
isJustSpawned = false;
$$anonymous$$oveBuildingObject();
}
private void Update() {
if(!isJustSpawned) return;
$$anonymous$$oveBuildingObject();
}
public void $$anonymous$$oveBuildingObject()
{
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,$$anonymous$$athf.Infinity))
{
transform.position = hit.point;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612144955im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
What's the difference between MonoBehavior OnMouseDrag and IDragHandler OnDrag 1 Answer
RawImage - Cannot update material's UV offset. 1 Answer
Unity2D : How can i change animation by dragging finger left or right on screen? 0 Answers