private bool ismove;//Mobile
The parameter of the mouse double-click (the first way parameter)
private float delay = 0.5f;
private float firstclicktime = 0;
private bool OneClick = false; Click on the first
Double-click (parameter of the second way)
private float endtime = 0;
private float doubletime = 0.5f; Response time
public void Start (Gameobject _player, Camera Cam)
public void Update ()
{
if (Input.getmousebuttondown (0))
{
Double click
float time = Time.realtimesincestartup;
if ((Endtime + doubletime) > Time)
{
Ismove = false;//Move Pause
Debug.logerror ("Mouse double-click");
}
Else
{
Endtime = time;
Debug.logerror ("mouse click");
Ismove = true;//begins to move
1. Get mouse click Position
To create a ray; emit a ray from the camera at the current position of the mouse
Ray Ray = Firstcamera.screenpointtoray (input.mouseposition);
Emission Ray
Raycasthit hitinfo = new Raycasthit ();
if (Physics.raycast (ray, out Hitinfo))
{
Get the location of the collision point
if (HitInfo.collider.name = = "Ground")
{
Debug.logerror (HitInfo.collider.name);
TargetVector3 = Hitinfo.point;
Targetvector3.y = -1f;
Isover = false;
}
Debug.drawline (Ray.origin, Hitinfo.point, color.red);
}
}
if (OneClick && time.time-firstclicktime < delay)
{
Double click
OneClick = false;
Ismove = false;//Move Pause
Debug.logerror ("Mouse double-click");
}
Else
{
Debug.logerror ("mouse click");
OneClick = true;
Ismove = true;//begins to move
Firstclicktime = Time.time;
1. Get mouse click Position
To create a ray; emit a ray from the camera at the current position of the mouse
Ray Ray = Firstcamera.screenpointtoray (input.mouseposition);
Emission Ray
Raycasthit hitinfo = new Raycasthit ();
if (Physics.raycast (ray, out Hitinfo))
{
Get the location of the collision point
if (HitInfo.collider.name = = "Ground")
{
Debug.logerror (HitInfo.collider.name);
TargetVector3 = Hitinfo.point;
Targetvector3.y = -1f;
Isover = false;
}
Debug.drawline (Ray.origin, Hitinfo.point, color.red);
}
}
}
if (Ismove)
{
2. To move a character to a target location
MoveTo (TARGETVECTOR3);
}
}
To move a character to a target location
private void MoveTo (Vector3 tar)
{
if (! Isover)
{
Vector3 offSet = tar-player.transform.position;
Player.transform.position + = offset.normalized * Movespeed * time.deltatime;
if (Vector3.distance (tar, player.transform.position) < 0.5f)
{
Isover = true;
Player.transform.position = tar;
}
}
}
Move objects to the mouse click position in unity (click Move and double tap to pause the move)