資源簡介
文章Unity3D學習筆記04:角色控制器CharacterController控制人物移動旋轉 的項目源碼,免費下載。

代碼片段和文件信息
using?UnityEngine;
using?System.Collections;
public?class?heroControl?:?MonoBehaviour?{
????//定義主角角色控制器
????CharacterController?hero;
????//點數組
????public?Transform[]?points;
????//下一個點的下標,主角移動速度
????public?int?nextIndex;
????public?int?moveSpeed?=?10;
????void?Start()
????{
????????//初始化主角移動速度
????????nextIndex?=?0;
????????//獲得主角的角色控制器組件
????????hero?=?GetComponent();
????}
????void?Update()
????{
????????//如果主角距離點的距離大于0.2,則算出主角的朝向,移動主角人物
????????if?(Vector3.Distance(ignoreY(points[nextIndex?%?points.Length].position)?ignoreY(transform.position))?>?0.2f)
????????{
????????????//主角的朝向即為下一個點坐標減去主角坐標的向量
????????????Vector3?direction?=?(ignoreY(points[nextIndex?%?points.Length].position)?-?ignoreY(transform.position)).normalized;
????????????//插值改變主角的朝向,使其有一個自然轉向的過程,防止其瞬間轉向
????????????hero.transform.forward?=?Vector3.Lerp(transform.forward?direction?0.1f);
????????????//移動主角
????????????hero.SimpleMove(transform.forward?*?moveSpeed);
????????}
????????else
????????{
????????????//如果到達點,則使下一點作為目標點
????????????nextIndex++;
????????}
????}
????//這個函數用來取消向量的Y軸影響,比如主角的高度與點之間可能有一段距離,我們要忽略這段距離
????Vector3?ignoreY(Vector3?v3)
????{
????????return?new?Vector3(v3.x?0?v3.z);
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2643??2013-08-21?21:41??Test2\Assembly-CSharp-vs.csproj
?????文件???????2643??2013-08-21?21:41??Test2\Assembly-CSharp.csproj
?????文件?????139680??2012-09-10?10:39??Test2\Assets\GodOfPunishment\GodOfPunishment.FBX
?????文件???????4224??2013-08-21?13:53??Test2\Assets\GodOfPunishment\Materials\GodOfPunishment.mat
????I.A....??????4228??2013-08-21?21:21??Test2\Assets\GodOfPunishment\Materials\T_GodOfPunishment?1.mat
?????文件???????4228??2013-08-21?13:54??Test2\Assets\GodOfPunishment\Materials\T_GodOfPunishment.mat
?????文件????2462852??2012-09-10?10:39??Test2\Assets\GodOfPunishment\T_GodOfPunishment.psd
?????文件???????1452??2013-08-21?21:54??Test2\Assets\heroControl.cs
????I.A....?????36072??2013-08-21?22:07??Test2\Assets\scene.unity
????I.A....??????4116??2013-08-21?21:15??Test2\Library\AnnotationManager
????I.A....?????26748??2013-08-21?22:07??Test2\Library\assetDataba
????I.A....?????????6??2013-08-21?21:15??Test2\Library\AssetImportState
????I.A....??????5044??2013-08-21?22:07??Test2\Library\AssetServerCacheV3
????I.A....?????????0??2013-08-21?21:15??Test2\Library\BuildPla
????I.A....??????4116??2013-08-21?21:15??Test2\Library\BuildSettings.asset
????I.A....??????4160??2013-08-21?21:15??Test2\Library\EditorUserBuildSettings.asset
????I.A....??????4108??2013-08-21?21:15??Test2\Library\EditorUserSettings.asset
????I.A....??????4132??2013-08-21?22:07??Test2\Library\expandedItems
????I.A....?????????0??2013-08-21?22:07??Test2\Library\FailedAssetImports.txt
????I.A....??????4636??2013-08-21?22:07??Test2\Library\guidmapper
????I.A....??????4144??2013-08-21?22:07??Test2\Library\InspectorExpandedItems.asset
????I.A....??????4268??2013-08-21?21:15??Test2\Library\me
????I.A....??????4304??2013-08-21?21:15??Test2\Library\me
????I.A....??????4296??2013-08-21?21:15??Test2\Library\me
????I.A....??????4308??2013-08-21?21:15??Test2\Library\me
????I.A....??????4308??2013-08-21?21:15??Test2\Library\me
????I.A....??????4296??2013-08-21?21:15??Test2\Library\me
????I.A....??????4304??2013-08-21?21:15??Test2\Library\me
????I.A....??????4304??2013-08-21?21:15??Test2\Library\me
????I.A....??????4308??2013-08-21?21:15??Test2\Library\me
............此處省略304個文件信息
- 上一篇:SmsDemo.rar
- 下一篇:數學模型第三版答案詳細版
評論
共有 條評論