Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question