public class Test001 : MonoBehaviour
{
public Transform Cube;
public LayerMask LM;//射线检测的层
Ray ray;
RaycastHit hit;//射线检测信息
private void Start()
{
hit.point = transform.position;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
;//从摄像机发出一条指向鼠标位置(鼠标的位置是屏幕坐标)的射线
ray = Camera.main.ScreenPointToRay(Input.mousePosition)
Physics.Raycast(ray, out hit, LM);
}
if (hit.point != null)
{
Cube.position = Vector3.MoveTowards(Cube.position, hit.point, Time.deltaTime);
}
}