public class FreeMove : MonoBehaviour
{
private static List<Vector3> listPos = new List<Vector3>();
private bool isMove = false;
private static int n = 0;
private float lookatTime = 4;
private float movetoTime = 10;
void Update()
{
//mouse click
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hits;
if(Physics.Raycast(ray, out hits))
{
if(hits.collider.gameObject.tag == "terr")
{
Vector3 vec = new Vector3(hits.point.x, hits.point.y + 0.5f, hits.point.z);
listPos.Add(vec);
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.transform.position = vec;
obj.collider.isTrigger = true;
obj.AddComponent<TriggerTest>();
}
}
}
}
void LateUpdate()
{
//camera move
if (isMove)
{
if (listPos != null)
{
//look at pos
this.gameObject.transform.LookAt(new Vector3(Mathf.Lerp(this.gameObject.transform.position.x, listPos[n].x, Time.deltaTime * lookatTime), Mathf.Lerp(this.gameObject.transform.position.y, listPos[n].y, Time.deltaTime * lookatTime), Mathf.Lerp(this.gameObject.transform.position.z, listPos[n].z, Time.deltaTime * lookatTime)));
//move to pos
Vector3 targetPos = listPos[n];
this.gameObject.transform.position = new Vector3(Mathf.Lerp(this.gameObject.transform.position.x, targetPos.x, Time.deltaTime * movetoTime), Mathf.Lerp(this.gameObject.transform.position.y, targetPos.y, Time.deltaTime * movetoTime), Mathf.Lerp(this.gameObject.transform.position.z, targetPos.z, Time.deltaTime * movetoTime));
}
}
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 80, 30), "WALK"))
{
isMove = true;
}
}
public static void AddNum()
{
if (n < listPos.Count)
{
n++;
}
}
}
public class TriggerTest : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
FreeMove.AddNum();
}
}