1. Move the object to the mouse click
2. Object follows mouse movement
Divided into three steps: Get mouse position, convert to world coordinates, object movement
Private Vector3 Mopos;
Private Vector3 Gamepos;
void Start () {
the world coordinates of the object are converted into screen coordinates.
Gamepos = Camera.main.WorldToScreenPoint (transform.position);
Gamepos = transform.position;
}
void Update () {
object moved to mouse click
if (Input.getmousebuttondown (0)) {
Mopos = input.mouseposition;// get mouse position
Convert object World z coordinate to screen coordinates and assign value to mouse z-coordinate
Mopos.z=camera.main.worldtoscreenpoint (transform.position). Z;
Convert the desired mouse coordinates to world coordinates
Gamepos.x=camera.main.screentoworldpoint (Mopos). x;
Gamepos.z=camera.main.screentoworldpoint (mopos). Z;
GAMEPOS.Y=TRANSFORM.POSITION.Y;
}
To move an object to a coordinate position
Transform.position = Vector3.lerp (gameobject.transform.position,gamepos,time.deltatime*5f);
Towards a place
Transform. LookAt (Gamepos);
}
Ps: A small example, the crosshair in the screen following the mouse movement
void Updata () {
Mopos = input.mouseposition;//Get mouse position
Adjust position scale
Mopos.x/= 823.0f;
Mopos.y/= 600.0f;
Move
Transform.position = Vector3.lerp (gameobject.transform.position,mopos,time.deltatime*30f);
}
Object rotates toward mouse direction
// Get mouse Increment
Float h = input.getaxis ("Mouse X");
Float v = input.getaxis ("Mouse Y");
Rotating
Transform. Rotate (V, h, 0);
Mouse Control Object Movement