自己写的一个简单的角色控制脚本,JS,学习学习
//此脚本主要用来控制角色的平移旋转,W\A\S\D控制角色平移,Q\E控制角色旋转,空格为跳跃//请轻喷,谢谢~~~!!
var speed : float = 20.0;
var jumpSpeed : float = 15.0;
var gravity : float = 20.0;
var rotateSpeed : float = 2.0;
var moveHorz : float = 0.0;
private var moveDirection : Vector3 = Vector3.zero;
private var rotateDirection : Vector3 = Vector3.zero;
var controller : CharacterController;
controller = GetComponent(CharacterController);
private var isControllable : boolean = true; //用于判断玩家是否能控制角色
private var isWalking : boolean = false;
private var isBacking : boolean = false;
private var isLeft : boolean = false;
private var isRight : boolean = false;
private var isJumping : boolean = false;
function Update () {
//先判断角色是否可控,如果否,则重设输入,继续执行
if(!isControllable)
Input.ResetInputAxes();
else {
if (controller.isGrounded) {
//我们着地了,所以直接通过轴重新计算move direction。
isJumping = false;
if(Input.GetAxis("Horizontal") != 0||Input.GetAxis("Vertical") !=0 ){
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetAxis("Vertical") > 0){
isWalking = true;
}
else if(Input.GetAxis("Vertical") < 0){
isBacking = true;
}
else if(Input.GetAxis("Horizontal") > 0){
isRight = true;
}
else if(Input.GetAxis("Horizontal") < 0){
isLeft = true;
}
}
else {
isWalking = false;
isBacking = false;
isLeft = false;
isRight = false;
moveDirection = Vector3.zero;
}
moveHorz = Input.GetAxis("Horizontal");
if(Input.GetKey(KeyCode.E)){
//正Y轴为旋转轴
rotateDirection = new Vector3(0,1,0);
}
else if(Input.GetKey(KeyCode.Q)){
//负Y轴为旋转轴
rotateDirection = new Vector3(0,-1,0);
}
else { rotateDirection = new Vector3(0,0,0); }
if(Input.GetButton("Jump")){
moveDirection.y = jumpSpeed;
}
}
else {
isJumping = true;
}
//应用重力。
moveDirection.y -= gravity * Time.deltaTime;
//移动控制器。
controller.Move(moveDirection * Time.deltaTime);
//以rotateDirection为轴,绕其旋转
controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
}
}
function IsWalking(){
return isWalking;
}
function IsBacking(){
return isBacking;
}
function IsLeft(){
return isLeft;
}
function IsRight(){
return isRight;
}
function IsJumping(){
return isJumping;
}
//将自定义的控制器放在Component里面,方便使用
@script AddComponentMenu("Players/Character_Controller");
不错 加油……………………
感谢楼主的无私分享! 楼主是超人 顶顶多好 真心顶 难得一见的好帖 不错不错 楼主是超人 真心顶