Unity —— 通过鼠标点击控制物体移动

 1 //ClickMove - - 通过鼠标点击控制物体移动
 2 
 3 using System.Collections;
 4 using System.Collections.Generic;
 5 using UnityEngine;
 6 using UnityEngine.AI;       // include NavMeshAgent
 7 
 8 public class ClickMove : MonoBehaviour {
 9 
10     public NavMeshAgent player;
11 
12     //获取动画组件
13     //public Animator anim;
14 
15     // Use this for initialization
16     void Start () {
17         
18     }
19     
20     // Update is called once per frame
21     void Update () {
22 
23         if(Input.GetMouseButtonDown(0))
24         {
25             //通过鼠标点击的位置生成射线
26             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
27             RaycastHit hit;
28             if(Physics.Raycast(ray,out hit))
29             {
30                 print(hit.point);
31                 player.SetDestination(hit.point);  //转递鼠标点击信息
32             }
33         }
34 
35         //控制动画的播放
36         //anim.SetFloat("Speed", player.velocity.magnitude);
37         
38     }
39 }
ClickMove - - 通过鼠标点击控制物体移动

猜你喜欢

转载自www.cnblogs.com/QQW123/p/9048778.html