make object move to the mouse clicked position.
using UnityEngine; using System.Collections;
public class butto : MonoBehaviour {
private bool flag = false;
private Vector2 endPoint;
public float duration = 50.0f;
private float yAxis;
void Start()
{
yAxis = gameObject.transform.position.y;
}
void Update()
{
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
RaycastHit hit;
Ray ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
flag = true;
endPoint = hit.point;
endPoint.y = yAxis;
Debug.Log(endPoint);
}
}
if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
gameObject.transform.position = Vector2.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector2.Distance(gameObject.transform.position, endPoint))));
}
else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
flag = false;
Debug.Log("I am here");
}
}
}
this is the code for 3d. please help me to change this for 2d it does not work.
Comment