Unity3D正常行走和飞行的实现
生活随笔
收集整理的這篇文章主要介紹了
Unity3D正常行走和飞行的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建一個TestFlay.js腳本,內容如下:
var speed = 6.0;
var jumpSpeed = 8.0;var gravity = 0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
my=moveDirection.y;
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
if (grounded) {
? moveDirection *= speed;
}
else {
? moveDirection *= speed+transform.position.y/5;
? moveDirection.y=my; ?
}
if(transform.position.y>=6){
if(Input.GetKey ("e")) {//按下e鍵向下飛行
? transform.Translate(0, -1, 0);? ?
}
}
if (Input.GetKey ("q") ) {?? //按下q鍵向上飛行
? transform.Translate(0, 1, 0);
}
if(Input.GetKey("left shift")) {
? speed=6*5;
} else {?????? ?
? speed=6;
}
moveDirection.y -= gravity * Time.deltaTime;??
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
private var myWalker: FPSWalker=null;
var maxHeight: float=250;
function Start () {
myWalker = gameObject.GetComponent(FPSWalker);
}
@script RequireComponent(FPSWalker)
將它綁定到First Person Controller上,然后將前面的對勾勾掉。
再建一個腳本Fly.js,內容為:
var play:GameObject;
function Update () {
}
function OnGUI(){
if(GUI.Button(Rect(215,280,75,30),"Normal-Walk")){
??? play.GetComponent("FPSWalker").enabled? = false;
??? play.GetComponent("TestFly").enabled? = true;
}
if(GUI.Button(Rect(230,200,50,30),"Start-Fly")){
??? play.GetComponent("FPSWalker").enabled? = true;
??? play.GetComponent("TestFly").enabled? = false;
}
}
將其綁定到一個新建的GameObject上,運行即可看到效果。
轉載于:https://www.cnblogs.com/Mygirl/archive/2011/04/02/2003105.html
總結
以上是生活随笔為你收集整理的Unity3D正常行走和飞行的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#.NET 比较好用的tcp通信模板(
- 下一篇: 在SharePoint 2010系统中安