分享

关于unity的animation的人物简单控制脚步

 雪柳花明 2016-03-28
Unity3D中Animation的常见属性及方法
[Actionscript3] 纯文本查看 复制代码
1
2
function Play (mode : PlayMode = PlayMode.StopSameLayer) : bool
function Play (animation : string, mode : PlayMode = PlayMode.StopSameLayer) : bool


Play()将开始播放名称为animation的动画,或者播放默认动画。动画会突然开始播放而没有任何混合。如果模式是PlayMode.StopSameLayer,那么所有在同一个层的动画将停止播放。如果模式是PlayMode.StopAll,那么所有当前在播放的动画将停止播放。

如果动画已经在播放过程中,别的动画将停止但是动画不会回退到开始位置。
如果动画没有被设置成循环模式,它将停止并且回退到开始位置
如果动画不能被播放(没有动画剪辑或者没有默认动画),Play()将返回false。


[Actionscript3] 纯文本查看 复制代码
1
2
3
4
5
6
7
// 播放默认动画。
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);


Animation.CrossFade淡入淡出
function CrossFade (animation : string, fadeLength : float = 0.3F, mode : PlayMode = PlayMode.StopSameLayer) : void


在一定时间内淡入名称为name的动画并且淡出其他动画。如果模式是PlayMode.StopSameLayer,在同一层的动画将在动画淡入的时候淡出。如果模式是PlayMode.StopAll,所有动画将在淡入的时候淡出。如果动画没有被设置成循环,它将停止并且在播放完成之后倒带至开始。

[Actionscript3] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
// 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");
}


这里列出的最常见的播放动画的两种方法,还有其他方法属性大家自觉的去查下API吧。下面我放出自己写的人物简单控制脚步,包括上下左右移动。j,k攻击。

RoleScriptContorl.cs

[Actionscript3] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
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);
  
    }
    
      
}


下面是动画状态的一个脚本类

[Actionscript3] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
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";
          

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多