Move objects to the mouse click position in unity (click Move and double tap to pause the move)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.