1. 程式人生 > >Unity —— 通過鼠標點擊控制物體移動

Unity —— 通過鼠標點擊控制物體移動

技術 aps this lap sep CA sha pub mon

技術分享圖片
 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 - - 通過鼠標點擊控制物體移動

Unity —— 通過鼠標點擊控制物體移動