K
K
Kt0T02020-12-16 18:56:01
Unity
Kt0T0, 2020-12-16 18:56:01

Moving an object with hold & drag?

Good day, help me cope with one task, how to move a 3D object along the x axis by holding and dragging with a finger on the screen or with the mouse?

Sorry for not being able to formulate the question correctly!

5fda2e0108091399183755.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2020-12-24
@Kt0T0

Assign a collider to an object and:

void OnMouseDrag() {
    Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) ; 
    mousePos.y = 0f; // только по Х
    transform.position = mousePos ; 
}
For the drag, we catch it with the TouchPhase.Moved phase. And also set the coordinate.
https://docs.unity3d.com/ScriptReference/Touch-pha...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question