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))
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ;//從攝像機發(fā)出一條指向鼠標位置(鼠標的位置是屏幕坐標)的射線
? ? ? ? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? ? }
}