自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Unity3D研究之鼠標(biāo)控制角色移動與奔跑示例

移動開發(fā) iOS 游戲開發(fā)
看到這個(gè)標(biāo)題我相信大家應(yīng)該并不陌生,一般在PC網(wǎng)絡(luò)游戲中玩家通過鼠標(biāo)左鍵在游戲世界中選擇角色目標(biāo)移動位置,接著主角將面朝點(diǎn)擊的那個(gè)方向移動。

 看到這個(gè)標(biāo)題我相信大家應(yīng)該并不陌生,一般在PC網(wǎng)絡(luò)游戲中玩家通過鼠標(biāo)左鍵在游戲世界中選擇角色目標(biāo)移動位置,接著主角將面朝點(diǎn)擊的那個(gè)方向移動。首先就本文來說我們應(yīng)當(dāng)掌握的知識點(diǎn)是“鼠標(biāo)揀選”。這是什么概念呢?其實(shí)很簡單,就是玩家通過鼠標(biāo)在Game視圖中選擇了一個(gè)點(diǎn),需要得到該點(diǎn)在3D世界中的三維坐標(biāo)系。Game視圖是一個(gè)2D的平面,所以鼠標(biāo)揀選的難點(diǎn)就是如何把一個(gè)2D坐標(biāo)換算成3D坐標(biāo)。我們可以使用射線的原理很好的解決這個(gè)問題,在平面中選擇一個(gè)點(diǎn)后從攝像機(jī)向該點(diǎn)發(fā)射一條射線。判斷:選擇的這個(gè)點(diǎn)是否為地面,如果是地面拿到這個(gè)點(diǎn)的3D坐標(biāo)即可。如下圖所示,在場景視圖中我們簡單的制作了帶坡度的地形,目標(biāo)是用戶點(diǎn)擊帶坡度或不帶坡度的地形都可以順利的到達(dá)目的地。

本文依然使用角色控制器組件,不知道這個(gè)組件的朋友請看MOMO之前的文章。因?yàn)楣俜教峁┑哪_本是JavaScript語言。MOMO比較喜歡C#所以放棄了在它的基礎(chǔ)上修改,而針對本文的知識點(diǎn)重寫編寫腳本,這樣也方便大家學(xué)習(xí),畢竟官方提供的代碼功能比較多,代碼量也比較多。廢話不多說了進(jìn)入正題,首先在將模型資源載入工程,這里沒有使用官方提供的包,而直接將模型資源拖拽入工程。如下圖所示,直接將角色控制器包中的模型資源拖拽如層次視圖當(dāng)中。

 
 
 
 
在Project視圖中鼠標(biāo)右鍵選擇Import  Package ->Script引入官方提供的腳本,這些腳本主要是應(yīng)用于攝像機(jī)朝向的部分。首先在Hierarchy視圖中選擇攝像機(jī)組件,接著在導(dǎo)航欄菜單中選擇Compont -> Camera-Control ->SmoothFollow腳本。實(shí)際意義是將跟隨腳本綁定在攝像機(jī)之上,目的是主角移動后攝像機(jī)也能跟隨主角一并移動。如下圖所示,腳本綁定完畢后可在右側(cè)監(jiān)測面板視圖中看到Smooth Follow腳本。Target 就是射向攝像機(jī)朝向的參照物,這里把主角對象掛了上去意思是攝像機(jī)永遠(yuǎn)跟隨主角移動。
 
 

由于官方提供的腳本并不是特別的好,攝像機(jī)永遠(yuǎn)照射在主角的后面,以至于控制主角向后回頭時(shí)也無法看到主角的面部表情,所以MOMO簡單的修改一下這條腳本,請注意一下我修改的地方即可。

SmootFollow.js

[代碼]js代碼:

01 // The target we are following
02 var target : Transform;
03 // The distance in the x-z plane to the target
04 var distance = 10.0;
05 // the height we want the camera to be above the target
06 var height = 5.0;
07 // How much we
08 var heightDamping = 2.0;
09 var rotationDamping = 3.0;
10  
11 // Place the script in the Camera-Control group in the component menu
12 @script AddComponentMenu("Camera-Control/Smooth Follow")
13  
14 function LateUpdate () {
15     // Early out if we don't have a target
16     if (!target)
17         return;
18  
19     // Calculate the current rotation angles
20     var wantedRotationAngle = target.eulerAngles.y;
21     var wantedHeight = target.position.y + height;
22  
23     var currentRotationAngle = transform.eulerAngles.y;
24     var currentHeight = transform.position.y;
25  
26     // Damp the rotation around the y-axis
27     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
28  
29     // Damp the height
30     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
31  
32     // Convert the angle into a rotation
33  
34     //下面是原始代碼。
35     //var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
36  
37     //這里是我修改的,直接讓它等于1,
38     //攝像機(jī)就不會旋轉(zhuǎn)。
39     var currentRotation = 1;
40  
41     // Set the position of the camera on the x-z plane to:
42     // distance meters behind the target
43     transform.position = target.position;
44     transform.position -= currentRotation * Vector3.forward * distance;
45  
46     // Set the height of the camera
47     transform.position.y = currentHeight;
48  
49     // Always look at the target
50     transform.LookAt (target);
51 }
OK ! 下面我們給主角模型添加角色控制器組件,請先把自帶的控制攝像機(jī)與鏡頭的控制腳本刪除。如下圖所示主角對象身上掛著Character Controller(角色控制器組件)即可,Controller是我們自己寫的腳本,用來控制主角移動。
 
下面看一下Controller.cs完整的腳本,腳本中我們將主角共分成三個(gè)狀態(tài):站立狀態(tài)、行走狀態(tài)、奔跑狀態(tài)。默認(rèn)情況下主角處于站立狀態(tài),當(dāng)鼠標(biāo)選擇一個(gè)目標(biāo)時(shí),主角將進(jìn)入行走狀態(tài)面朝目標(biāo)方向行走。當(dāng)連續(xù)按下鼠標(biāo)左鍵時(shí)主角將進(jìn)入奔跑狀態(tài)朝向目標(biāo)方向奔跑。

[代碼]js代碼:

001 using UnityEngine;
002 using System.Collections;
003  
004 public class Controller : MonoBehaviour
005 {
006  
007     //人物的三個(gè)狀態(tài) 站立、行走、奔跑
008     private const int HERO_IDLE = 0;
009     private const int HERO_WALK = 1;
010     private const int HERO_RUN = 2;
011  
012     //記錄當(dāng)前人物的狀態(tài)
013     private int gameState = 0;
014  
015     //記錄鼠標(biāo)點(diǎn)擊的3D坐標(biāo)點(diǎn)
016     private Vector3 point;
017     private float time;
018  
019     void Start ()
020     {
021         //初始設(shè)置人物為站立狀態(tài)
022         SetGameState(HERO_IDLE);
023  
024     }
025  
026     void Update ()
027     {
028         //按下鼠標(biāo)左鍵后
029         if(Input.GetMouseButtonDown(0))
030         {
031             //從攝像機(jī)的原點(diǎn)向鼠標(biāo)點(diǎn)擊的對象身上設(shè)法一條射線
032             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
033             RaycastHit hit;
034             //當(dāng)射線彭轉(zhuǎn)到對象時(shí)
035             if (Physics.Raycast(ray, out hit))
036             {
037                 //目前場景中只有地形
038                 //其實(shí)應(yīng)當(dāng)在判斷一下當(dāng)前射線碰撞到的對象是否為地形。
039  
040                 //得到在3D世界中點(diǎn)擊的坐標(biāo)
041                 point = hit.point;
042  
043                 //設(shè)置主角面朝這個(gè)點(diǎn),主角的X 與 Z軸不應(yīng)當(dāng)發(fā)生旋轉(zhuǎn),
044                 //注解1
045                 transform.LookAt(new Vector3(point.x,transform.position.y,point.z));
046  
047                 //用戶是否連續(xù)點(diǎn)擊按鈕
048                 if(Time.realtimeSinceStartup - time <=0.2f)
049                 {
050                         //連續(xù)點(diǎn)擊 進(jìn)入奔跑狀態(tài)
051                         SetGameState(HERO_RUN);
052                 }else
053                 {
054                         //點(diǎn)擊一次只進(jìn)入走路狀態(tài)
055                         SetGameState(HERO_WALK);
056                 }
057  
058                 //記錄本地點(diǎn)擊鼠標(biāo)的時(shí)間
059                 time = Time.realtimeSinceStartup;
060             }
061         }
062     }
063  
064     void FixedUpdate()
065     {
066  
067         switch(gameState)
068         {
069         case HERO_IDLE:
070  
071             break;
072         case HERO_WALK:
073             //移動主角 一次移動長度為0.05
074             Move(0.05f);
075             break;
076  
077         case HERO_RUN:
078             //奔跑時(shí)移動的長度為0.1
079             Move(0.1f);
080             break;
081         }
082  
083     }
084  
085     void SetGameState(int  state)
086     {
087         switch(state)
088         {
089         case HERO_IDLE:
090             //播放站立動畫
091             point = transform.position;
092             animation.Play("idle");
093             break;
094         case HERO_WALK:
095             //播放行走動畫
096             animation.Play("walk");
097             break;
098         case HERO_RUN:
099             //播放奔跑動畫
100             animation.Play("run");
101             break;
102         }
103         gameState = state;
104     }
105  
106     void Move(float speed)
107     {
108  
109         //注解2
110         //主角沒到達(dá)目標(biāo)點(diǎn)時(shí),一直向該點(diǎn)移動
111         if(Mathf.Abs(Vector3.Distance(point, transform.position))>=1.3f)
112         {
113             //得到角色控制器組件
114             CharacterController controller  = GetComponent();
115             //注解3 限制移動
116             Vector3 v = Vector3.ClampMagnitude(point -  transform.position,speed);
117             //可以理解為主角行走或奔跑了一步
118             controller.Move(v);
119         }else
120         {
121             //到達(dá)目標(biāo)時(shí) 繼續(xù)保持站立狀態(tài)。
122             SetGameState(HERO_IDLE);
123         }
124     }
125  
126 }

注解1:transform.LookAt()這個(gè)方法是設(shè)定主角對象的面朝方向,這里設(shè)定的方向是鼠標(biāo)選擇的目標(biāo)點(diǎn)在游戲世界中點(diǎn)中的3D坐標(biāo)。為了避免主角X與Z軸發(fā)生旋轉(zhuǎn)(特殊情況)所以我們設(shè)定朝向的Y軸永遠(yuǎn)是主角自身的Y軸。

注解2:在這里判斷主角當(dāng)前位置是否到達(dá)目標(biāo)位置,然后取得兩點(diǎn)坐標(biāo)差的絕對值。未到達(dá)目的繼續(xù)向前行走或奔跑,達(dá)到目的主角進(jìn)入站立狀態(tài)等待下一次移動。
注解3:在選中目標(biāo)點(diǎn)后主角并不是直接移動過去,應(yīng)當(dāng)是經(jīng)過一段行走或奔跑的時(shí)間才移動過去。所以我們需要得知主角行走或奔跑下一步的坐標(biāo),那么通過 Vertor3.ClampMagnitude()方法即可取得。參數(shù)1為兩個(gè)坐標(biāo)點(diǎn)之間的距離差,參數(shù)2表示行走或奔跑一步的距離,最后通過角色控制器組件提供的Move方法來移動主角。

 

如上圖所示,雙擊鼠標(biāo)在3D中選擇了一個(gè)目標(biāo)點(diǎn),主角正在努力的向該點(diǎn)奔跑。
工程的下載地址如下:http://115.com/file/c2lriwey#mouse.unitypackage
 
責(zé)任編輯:冰凝兒
相關(guān)推薦

2012-12-24 09:13:23

iOSUnity3D

2012-12-24 08:52:44

iOSUnity3D

2012-12-24 09:09:27

AndoidUnity3D

2012-12-24 09:20:48

AndoidUnity3D

2014-04-17 11:05:30

2012-12-24 09:19:31

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 09:08:14

iOSUnity3D

2013-06-17 09:12:31

Unity3D

2012-12-24 09:17:12

iOSUnity3D

2013-04-25 09:56:24

unity3D手機(jī)游戲引擎

2013-04-25 10:03:07

unity3D手機(jī)游戲引擎

2012-12-24 09:55:15

iOSUnity3D

2012-12-24 08:54:47

iOSUnity3D

2012-12-24 09:18:22

iOSUnity3D

2013-04-25 13:27:11

unity3D手機(jī)游戲引擎

2012-12-24 08:59:13

iOSUnity3D

2012-12-24 09:21:45

iOSUnity3D

2012-12-24 09:14:31

ios

2013-06-14 09:54:04

Unity3D
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號