Using Unityengine;
Using System.Collections;
public class Movetoclick:monobehaviour
{
Public Gameobject play;
public Vector3 tempos;
public bool ismoving;
public quaternion rotation;
Use this for initialization
void Start ()
{
Play = Gameobject.find ("Hero");
print (play);
}
Update is called once per frame
void Update ()
{
if (Input.getmousebuttondown (0))
{
Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition);
Raycasthit hit;
if (Physics.raycast (Ray, out hit, 100))
{
Print (hit.collider.name);
if (Hit.collider.gameObject.name = = "Plane")
{
Debug.Log (Hit.point);
Tempos = new Vector3 (Hit.point.x, PLAY.TRANSFORM.POSITION.Y, hit.point.z);
if (Vector3.distance (play.transform.position, Tempos) > 0.1)
{
Ismoving = true;
}
}
}
}
if (Vector3.distance (play.transform.position, tempos) <= 0.1)
{
Ismoving = false;
}
if (ismoving)
{
Turn (tempos);
This. Getcomponent<charactercontroller> (). Simplemove ((tempos-play.transform.position). normalized * 5f);
}
}
void Turn (Vector3 look)
{
rotation = Quaternion.lookrotation (tempos-play.transform.position, vector3.up);
Play.transform.rotation = Quaternion.slerp (play.transform.rotation, rotation, time.deltatime * 6.0f);
}
}
Unity3d object move to click Position