Z
Z
Zefirot2021-06-10 20:17:05
Unity
Zefirot, 2021-06-10 20:17:05

How to make the movement of an object equal to the mouse?

Here I did almost what I need, it only moves slowly, it needs to be at the speed of the mouse, in general the essence is this, there is a panel and image objects on it, and now I need to move this panel in different directions with the mouse

public GameObject NewGameObject;
public Camera cam;
private float startx,starty, SpeedChange1;
private Vector3 dir1,dir2;

void Start () {
  SpeedChange1 = NewGameObject.transform.position.z-cam.transform.position.z;
  }

private void Update(){
  if (Input.GetMouseButtonDown (0)) {    
    startx = NewGameObject.transform.position.x;
    starty = NewGameObject.transform.position.y;
    dir1 = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, SpeedChange1));
    } 
 if (Input.GetMouseButton (0)) {
    dir2 = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, SpeedChange1));
    NewGameObject.transform.position = new Vector3((dir2.x-dir1.x)+startx, (dir2.y-dir1.y)+starty,
      NewGameObject.transform.position.z);
      }           
  }

It moves but slowly...
How to do it right?
Canvas - Screen Space Overlay
Scale with....

In short, there is an interface that cannot be touched, but there is a map that can be moved and enlarged....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kachan, 2021-06-12
@Zefirot

You need to use raycast - you throw a ray, look where it hit and transfer the object there. Accordingly, there must be some kind of transparent substrate in order to move along its plane.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question