新建一个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上,运行即可看到效果。