- function Play (mode : PlayMode = PlayMode.StopSameLayer) : bool
- function Play (animation : string, mode : PlayMode = PlayMode.StopSameLayer) : bool
如果动画已经在播放过程中,别的动画将停止但是动画不会回退到开始位置。
如果动画没有被设置成循环模式,它将停止并且回退到开始位置
如果动画不能被播放(没有动画剪辑或者没有默认动画),Play()将返回false。
- // 播放默认动画。
- animation.Play();// Plays the walk animation - stops all other animations in the same layer
- // 播放walk动画 - 停止同一层的其他动画。
- animation.Play("walk");
- // Plays the walk animation - stops all other animations
- // 播放walk动画 - 停止其他动画。
- animation.Play("walk", PlayMode.StopAll);
function CrossFade (animation : string, fadeLength : float = 0.3F, mode : PlayMode = PlayMode.StopSameLayer) : void
在一定时间内淡入名称为name的动画并且淡出其他动画。如果模式是PlayMode.StopSameLayer,在同一层的动画将在动画淡入的时候淡出。如果模式是PlayMode.StopAll,所有动画将在淡入的时候淡出。如果动画没有被设置成循环,它将停止并且在播放完成之后倒带至开始。
- // Fade the walk cycle in and fade all other animations in the same layer out.
- // 淡入walk循环并且淡出同一层的所有其他动画。
- // Complete the fade within 0.2 seconds.
- // 在0.2秒之内完成淡入淡出。
- animation.CrossFade("Walk", 0.2);
- // Makes a character contains a Run and Idle animation
- // fade between them when the player wants to move
- // 让一个角色包含Run和Idle动画,并且在玩家想移动的时候在他们之间淡入淡出。
- function Update () {
- if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
- animation.CrossFade("Run");
- else
- animation.CrossFade("Idle");
- }
RoleScriptContorl.cs
- using UnityEngine;
- using System.Collections;
- public class PlayerControl : MonoBehaviour
- {
- public static PlayerControl instance;
- private AnimationClip animationClip;
- private CharacterController controller;
- public float runSpeed = 5;
- private float moveSpeed = 3;//走路速度
- private string currentState="";
- private GameObject monster;
- void Awake()
- {
- instance = this;
- }
- void Start ()
- {
- controller = GetComponent<CharacterController>();
- print(transform.forward);
- }
- void Update ()
- {
- KeyboardControl();
- }
- void KeyboardControl()
- {
- Vector3 forward = transform.TransformDirection(Vector3.forward);//从自身坐标转换为世界坐标
- if(Input.GetKey(KeyCode.W))
- {
- if(Input.GetKey(KeyCode.LeftShift))
- {
- currentState = RoleAnimationState.RUN;//改变人物动画状态
- PlayAnimation(currentState);//播放动画
- controller.SimpleMove(forward * runSpeed * Time.deltaTime*100);
- }
- else
- {
- currentState = RoleAnimationState.WALK;
- PlayAnimation(currentState);
- controller.SimpleMove(forward * moveSpeed * Time.deltaTime * 100);
- }
- }
- if (Input.GetKey(KeyCode.S))
- {
- Vector3 back = transform.TransformDirection(Vector3.back);
- currentState = RoleAnimationState.WALK;
- PlayAnimation(currentState);
- controller.SimpleMove(back * moveSpeed * Time.deltaTime * 100);
- }
- if (Input.GetKey(KeyCode.A))
- {
- Vector3 up = transform.TransformDirection(Vector3.up);
- transform.Rotate(Vector3.up * -50 * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.D))
- {
- Vector3 up = transform.TransformDirection(Vector3.up);
- transform.Rotate(Vector3.up * 50 * Time.deltaTime);
- }
- if (Input.GetKey(KeyCode.J))
- {
- currentState = RoleAnimationState.ATTACK;
- PlayAnimation(RoleAnimationState.ATTACK);
- }
- if (Input.GetKey(KeyCode.K))
- {
- currentState = RoleAnimationState.ATTACK01;
- PlayAnimation(RoleAnimationState.ATTACK01);
- }
- if(currentState!=RoleAnimationState.IDLE && !animation.IsPlaying(currentState))
- {
- PlayAnimation(RoleAnimationState.IDLE);
- }
- }
- void PlayAnimation(string name)
- {
- currentState = name;
- animationClip = animation[name].clip;
- gameObject.animation.CrossFade(animationClip.name);
- }
- }
- using UnityEngine;
- using System.Collections;
- public class RoleAnimationState : MonoBehaviour
- {
- public const string IDLE="Idle";
- public const string WALK = "Walk";
- public const string RUN = "Run";
- public const string ATTACK = "Attack";
- public const string ATTACK01 = "Attack01";
- }