Hello beloved community, I am sorry if this problem turns out to be dumb but I have now been stuck with it for over 3 hours, so I hope you can help me.
My scene
By clicking on a UI Image (with the Eventsystem OnPointerDown function), a 3D object gets spawned at the UI Image location. This Object can than be dragged around the scene. video: https://www.youtube.com/watch?v=8zik2eDTNwk&feature=youtu.be
The Problem
At the moment there are 2 Clicks needed. 1 Click to instatiate the object and 1 Click to drag it around.
I would like to have that happen in one Click only, So that my OnDrag() function gets called with the Instatiate() function. So that you can basicly drag the object from the UI in to the scene.
my way of thinking & what dint work
I tried a lot of diffrent things for example calling the Drag function by script or changing the way the UI Image gets pressed/activated. But in the end there are always 2 clicks needed. This make not so much sense to me, because the second click can also be above the raycast-blocking Image but still activates the OnDrag function (see second part of the video). Is there a reason why the OnDrag function doesnt get called in the first Click? and how do I work around it.
scripts
I provided the scripts in a comment below, I dont think they are part of the problem but you never know.
I appreciate every answer or also just some keywords which I can try to google for. As always thank you in advance, feel free to ask if something is not clear.
The best wishes from Switzerland.
snow2405.
Actually, I can’t answer on your certain question about drag function, however I can suggest you scripts that you can see below to workaround. They are instantiating the object and move it with 1 click and also you can move object after instantiating.
Script for instantiating and move:
using UnityEngine;
public class InstaMove : MonoBehaviour
{
public GameObject[] prefabs;
public Transform canvasParent;
Transform t_CurrentMoveObj;
bool b_IsMoving = false;
Vector3 v3_MouseOffset;
public void InstatiateDragable(int index)
{
/* Instantiate and assign to mouse position */
GameObject gm = Instantiate(prefabs[index], Input.mousePosition, Quaternion.identity, canvasParent);
/* Assign varaible so will be able to move later instantiated obj with this script */
gm.GetComponent<MoveSenter>().instaMove_ = this;
t_CurrentMoveObj = gm.transform;
MoveObject(t_CurrentMoveObj);
}
private void Update()
{
/* Movin object at instantiate or when press mouse button down over object */
if (b_IsMoving)
{
t_CurrentMoveObj.position = Input.mousePosition - v3_MouseOffset;
/* Stop move when mouse button is up */
if (Input.GetMouseButtonUp(0))
{
b_IsMoving = false;
t_CurrentMoveObj = null;
}
}
}
public void MoveObject(Transform transformObj)
{
t_CurrentMoveObj = transformObj;
b_IsMoving = true;
/* Calculate mouse offset from center of moving obj */
v3_MouseOffset = Input.mousePosition - t_CurrentMoveObj.position;
}
}
Script on instantiated object to contact with script that calculate movement:
using UnityEngine;
public class MoveSenter : MonoBehaviour
{
public InstaMove instaMove_;
/* Set up event trigger system on mouse button down */
public void AllowMove()
{
instaMove_.MoveObject(transform);
}
}
If you have any questions regarding to these scripts - feel free to ask