角色控制-附在物体表面运动-根据表面法线方向
private var character :CharacterController;
private var speed =0.0;
private var trans:Transform;
private var targetRot:Quaternion;
var rotationSpeed = 90 ;
var pedalImpulse = 3.5;
var maxSpeed = 12;
var decayRate = 0.1;
function Start()
{
character = GetComponent(CharacterController);
trans= transform ;
}
function pedal()
{
speed+= pedalImpulse;
speed = Mathf.Min(speed , maxSpeed);
}
function Update ()
{
var horizontal = Input.GetAxis("Horizontal");
transform.Rotate(0,rotationSpeed*horizontal*Time.deltaTime,0);
if(character.isGrounded && Input.GetKeyDown(KeyCode.Space))
pedal();
var moveDirection = trans.forward*speed;
moveDirection += Physics.gravity;
character.Move(moveDirection*Time.deltaTime);
var ray = new Ray(trans.position+Vector3.up,-Vector3.up);
var hit : RaycastHit;
if (character.isGrounded && Physics.Raycast(ray,hit,10))
{
var targetRight = Vector3.Cross(hit.normal,trans.forward);
var targetForward = Vector3.Cross(targetRight,hit.normal);
targetRot = Quaternion.LookRotation(targetForward,hit.normal);
}
trans.rotation = Quaternion.Slerp(trans.rotation,targetRot,5*Time.deltaTime);
if(character.isGrounded)
{
if(speed<0.3)
speed=0;
else
{
speed -= decayRate*Time.deltaTime*speed;
}
}
}