看到這個(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 |
05 |
// the height we want the camera to be above the target |
08 |
var heightDamping = 2.0; |
09 |
var rotationDamping = 3.0; |
11 |
// Place the script in the Camera-Control group in the component menu |
12 |
@script AddComponentMenu("Camera-Control/Smooth Follow") |
14 |
function LateUpdate () { |
15 |
// Early out if we don't have a target |
19 |
// Calculate the current rotation angles |
20 |
var wantedRotationAngle = target.eulerAngles.y; |
21 |
var wantedHeight = target.position.y + height; |
23 |
var currentRotationAngle = transform.eulerAngles.y; |
24 |
var currentHeight = transform.position.y; |
26 |
// Damp the rotation around the y-axis |
27 |
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); |
30 |
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); |
32 |
// Convert the angle into a rotation |
35 |
//var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); |
38 |
//攝像機(jī)就不會旋轉(zhuǎn)。 |
39 |
var currentRotation = 1; |
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; |
46 |
// Set the height of the camera |
47 |
transform.position.y = currentHeight; |
49 |
// Always look at the target |
50 |
transform.LookAt (target); |
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代碼:
002 |
using System.Collections; |
004 |
public class Controller : MonoBehaviour |
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; |
012 |
//記錄當(dāng)前人物的狀態(tài) |
013 |
private int gameState = 0; |
015 |
//記錄鼠標(biāo)點(diǎn)擊的3D坐標(biāo)點(diǎn) |
016 |
private Vector3 point; |
021 |
//初始設(shè)置人物為站立狀態(tài) |
022 |
SetGameState(HERO_IDLE); |
029 |
if(Input.GetMouseButtonDown(0)) |
031 |
//從攝像機(jī)的原點(diǎn)向鼠標(biāo)點(diǎn)擊的對象身上設(shè)法一條射線 |
032 |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); |
034 |
//當(dāng)射線彭轉(zhuǎn)到對象時(shí) |
035 |
if (Physics.Raycast(ray, out hit)) |
038 |
//其實(shí)應(yīng)當(dāng)在判斷一下當(dāng)前射線碰撞到的對象是否為地形。 |
040 |
//得到在3D世界中點(diǎn)擊的坐標(biāo) |
043 |
//設(shè)置主角面朝這個(gè)點(diǎn),主角的X 與 Z軸不應(yīng)當(dāng)發(fā)生旋轉(zhuǎn), |
045 |
transform.LookAt(new Vector3(point.x,transform.position.y,point.z)); |
047 |
//用戶是否連續(xù)點(diǎn)擊按鈕 |
048 |
if(Time.realtimeSinceStartup - time <=0.2f) |
050 |
//連續(xù)點(diǎn)擊 進(jìn)入奔跑狀態(tài) |
051 |
SetGameState(HERO_RUN); |
054 |
//點(diǎn)擊一次只進(jìn)入走路狀態(tài) |
055 |
SetGameState(HERO_WALK); |
058 |
//記錄本地點(diǎn)擊鼠標(biāo)的時(shí)間 |
059 |
time = Time.realtimeSinceStartup; |
085 |
void SetGameState(int state) |
091 |
point = transform.position; |
092 |
animation.Play("idle"); |
096 |
animation.Play("walk"); |
100 |
animation.Play("run"); |
106 |
void Move(float speed) |
110 |
//主角沒到達(dá)目標(biāo)點(diǎn)時(shí),一直向該點(diǎn)移動 |
111 |
if(Mathf.Abs(Vector3.Distance(point, transform.position))>=1.3f) |
114 |
CharacterController controller = GetComponent(); |
116 |
Vector3 v = Vector3.ClampMagnitude(point - transform.position,speed); |
121 |
//到達(dá)目標(biāo)時(shí) 繼續(xù)保持站立狀態(tài)。 |
122 |
SetGameState(HERO_IDLE); |
注解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