这个挺繁杂,嘿嘿,主要是为自己记录下啦。大家只需要注意下crossFade和animation的相关函数就可以了。
#pragma strict
private var Controller : CH_Character;
private var Controller_other : CH_Controller;
public var forwardSpeed : float = 5.0;
public var backwardSpeed : float = 3.0;
public var strafingSpeed : float = 4.0;
public var runningSpeed : float = 10.0;
public var idleAnimationSpeed : float = 1.0;
public var forwardAnimationSpeed : float = 6.0;
public var runningAnimationSpeed : float = 3.0;
public var backwardAnimationSpeed : float = 1.0;
public var strafingAnimationSpeed : float = 3.0;
public var jumpingAnimationSpeed : float = 1.5;
public var ShootingAnimationSpeed : float = 1.0;
function Start () {
animation.AddClip(animation["shoot"].clip, "shootUpperBody");
animation.AddClip(animation["shoot2"].clip, "shootUpperBody2");
animation["shootUpperBody"].AddMixingTransform(transform.Find("COG/Spine"));
animation["shootUpperBody2"].AddMixingTransform(transform.Find("COG/Spine"));
animation["idle"].layer = -1;
animation["run"].layer = -1;
animation["jump"].layer = -1;
animation["walk_forward"].layer = -1;
animation["walk_backward"].layer = -1;
animation["walk_side"].layer = -1;
animation.wrapMode = WrapMode.Loop;
animation["jump"].wrapMode = WrapMode.ClampForever;
animation["shoot"].wrapMode = WrapMode.Once;
animation["shoot2"].wrapMode = WrapMode.Once;
animation["shootUpperBody"].wrapMode = WrapMode.Once;
animation["shootUpperBody2"].wrapMode = WrapMode.Once;
animation["idle"].speed = idleAnimationSpeed;
animation["walk_forward"].speed = forwardAnimationSpeed;
animation["run"].speed = runningAnimationSpeed;
animation["walk_backward"].speed = backwardAnimationSpeed;
animation["walk_side"].speed = strafingAnimationSpeed;
animation["jump"].speed = jumpingAnimationSpeed;
animation["shootUpperBody"].speed = ShootingAnimationSpeed;
animation["shootUpperBody2"].speed = ShootingAnimationSpeed;
animation.Stop();
animation.Play("idle");
}
function DetermineDirection () {
if (Controller_other.bIsShooting) {
if (!animation.IsPlaying("shootUpperBody"))
animation.Play("shootUpperBody");
}
if (Controller_other.bIsShootingAlt) {
if (!animation.IsPlaying("shootUpperBody2"))
animation.Play("shootUpperBody2");
}
if (Controller.inAir) {
if (!Controller.jumpClimax) {
animation.CrossFade("jump", 0.5, PlayMode.StopSameLayer);
}
else {
animation.Rewind("jump");
}
}
else if (Controller.MoveDirection == Vector3.zero) {
animation.CrossFade("idle");
}
else if (Controller.MoveDirection.z > 0) {
if (Controller.isRunning) {
Controller.Speed = runningSpeed;
animation.CrossFade("run", 0.5, PlayMode.StopSameLayer);
}
else {
Controller.Speed = forwardSpeed;
animation.CrossFade("walk_forward", 0.5, PlayMode.StopSameLayer);
}
}
else if (Controller.MoveDirection.z < 0) {
Controller.Speed = backwardSpeed;
animation.CrossFade("walk_backward", 0.5, PlayMode.StopSameLayer);
}
else if (Controller.MoveDirection.x > 0 || Controller.MoveDirection.x
< 0) {
Controller.Speed = strafingSpeed;
animation.CrossFade("walk_side", 0.5, PlayMode.StopSameLayer);
}
}
function Awake () {
Controller = this.gameObject.GetComponent(CH_Character);
Controller_other = this.gameObject.GetComponent(CH_Controller);
}
function Update () {
DetermineDirection();
}