I want to know how to move the object behind the cursor (when the mouse is clamped on the object). Moreover, I still need to only drag a certain item, how to make a condition that checks it, and generally how to write the OnMouseDown method? Thank you in advance.
3 answers
If you are using 2d space (2d game or canvas), then there are interfaces IBeginDragHandler , IDragHandler , IEndDragHandler
I OnDrag(EventData eventData) that in the OnDrag(EventData eventData) method OnDrag(EventData eventData) there is a variable eventData and with the help of it you can move the object on which the ray fell (here you don’t need to start the ray yourself).
In order to move the object behind the mouse cursor, you need to write
eventData.pointerCurrentRaycast.gameObject.transform.position = Input.mousePosition;
For a start, how to identify an object. Here you should read how to throw the rays. Here is an example of a beam that rushes from the position of the mouse.
If(Input.GetMouseButtonDown(0))
From the name of the method it is clear that he is responsible for clicking on the mouse button. The index in the parameters is responsible for which button:
- 0 left mouse button
- 1 right mouse button
- 2 like a wheel
To cast a ray, you need to declare a ray variable.
Ray ray;
Now convert the screen point to the ray.
ray = Camera.main.ScreenPointToRay(Input.mousePosition));
Now you need the result of crossing the beam.
RaycastHit hit;
Now run the ray created above.
If(Physics.Raycast(ray,out hit))
If it was better thrown then this condition will work. And then the hit variable keeps the data from a collision with something. For example, you can get an object with which he crossed.
hit.collider.gameObject
And with this you can do anything. Since you want to drag the object after the mouse, you need to put all the above code in the condition:
if(Input.GetMouseButton(0))
This condition is responsible for the fact that the mouse button is clamped. We also throw a ray to determine that we are moving the same object. Well, respectively, mix it as you want. You can optimize by reducing the number of rays with additional conditions. The scope for action is huge. But the essence is the same.
- Be kind, format your answers, it is impossible to read. - RiotBr3aker
- With pleasure, but since I wrote it from the phone, it was problematic - Andrey
- IMHO it is better to "finish" to the PC, than to give out a correct, but unreadable answer though :) - RiotBr3aker
- I remind you that I work not with UI but with sprites, and the code should be for 2D. No answer helped :( - Spasibo
- To make your life easier and find only the required objects, you can assign a separate layer to objects and look for the layer mask docs.unity3d.com/ScriptReference/Physics.Raycast.html and
LayerMaskcan be made a separate variable in the script that reycast to make it convenient to configure - KingPeas
using UnityEngine; using System.Collections; public class DragDrop : MonoBehaviour { private Vector3 offset; void OnMouseDown() { offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f)); } void OnMouseDrag() { Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f); transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset; } } just hang the script on the object. The answer is taken from here: https://stackoverflow.com/a/38407823/4423545