if(Input.GetMouseButtonDown(0)){ RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit, Mathf.Infinity)){ agent.SetDestination(hit.point); } }
判断落点,只有是地面才移动(地面打Tag)
1 2 3 4 5 6 7 8 9
if(Input.GetMouseButtonDown(0)){ RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit, Mathf.Infinity)){ if(hit.transform.tag == "Floor"){ agent.SetDestination(hit.point); } } }
点击特效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// click to navigate if(Input.GetMouseButtonDown(0)){ RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit, Mathf.Infinity)){ // move when hit floor if(hit.transform.tag == "Floor"){ agent.SetDestination(hit.point);
// rotate camera up and down cameraRotation -= mouseY; cameraRotation = Mathf.Clamp(cameraRotation, -30f, 30f); cameraTransform.localRotation = Quaternion.Euler(cameraRotation, 0f, 0f);
// rotate player left and right transform.Rotate(Vector3.up * (mouseX));
此时长按拖拽和点击寻路会混,加一个mouseDownCount计数
缩放前后移动
1 2 3 4 5 6
// stop navigation agent.ResetPath();
// scroll to move forward and backward Vector3 move = transform.forward * Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity; agent.Move(move * Time.deltaTime);
void Update() { if(Input.GetMouseButtonUp(0)){ // if is click if(mouseDownCount < 2){ // click to navigate RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit, Mathf.Infinity)){ // move when hit floor if(hit.transform.tag == "Floor"){ agent.SetDestination(hit.point);
// rotate camera up and down cameraRotation -= mouseY; cameraRotation = Mathf.Clamp(cameraRotation, -30f, 30f); cameraTransform.localRotation = Quaternion.Euler(cameraRotation, 0f, 0f);
// rotate player left and right transform.Rotate(Vector3.up * (mouseX));