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 - - 通過鼠標點擊控制物體移動

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM