六.角色动画

要为角色添加身体的各个部分

将该预制件拖到hierachy面板,使其成为已有角色的子集,要记得调整其sorting Layer

这时Robbie身上会出现虚影,因为两个都显示出来了。在原来Robbie的inspector的面板中取消勾选Sprite Render

为角色添加动画效果

Windows》Animation》打开animation和animator

在animator窗口中,按住ALT键且按住鼠标可以调整位置,滚动鼠标滚轮可以放大缩小

角色动画脚本PlayerAnimation,拖拽到Robbie_Body上

为角色动画添加音乐

点击该动画,在其animator上点击add event,添加Audio

添加起跳到下落

因为该完整动作由7各小动作构成,

Animator>Mid Air>create new blend tree

在其inspector>add motion filed(添加7个)

parameter修改为verticalvelocity,让动画根据纵向加速度来改变,

将动画添加到motion filed,要调整Threshc来调整每个动画的纵向加速度

代码
public Component GetComponentInParent (Type type);
public Component GetComponentInParent (Type type, bool includeInactive);

参数

type要查找的组件的类型。

返回

Component 如果发现与类型匹配的组件,则返回组件。否则返回 null。

描述

获取 GameObject 或其任何父项中 Type type 的组件。

此方法向上递归,直到其找到具有匹配组件的 GameObject 为止。仅匹配活动 GameObjects 上的组件。

Animator.StringToHash("isOnGround");//将字符型转换为整数型

public void SetFloat (string name, float value);

public void SetFloat (string name, float value, float dampTime, float deltaTime);

public void SetFloat (int id, float value);

public void SetFloat (int id, float value, float dampTime, float deltaTime);

参数

name参数名称。
id参数 ID。
value新的参数值。
dampTime阻尼器总时间。
deltaTime给予阻尼器的增量时间。

描述

将浮点值发送到动画器以影响过渡。

在脚本中使用 SetFloat 将浮点值发送到动画器以激活过渡。在动画器中,定义哪些值如何影响某些动画的过渡方式。这在很多情况下都很有用,尤其是在动画循环中,例如在移动动画中可需要根据所施加的按钮压力来决定走或跑。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimation : MonoBehaviour
{
    Animator anim;
    PlayerMovement movement;
    Rigidbody2D rb;

    int groundID;
    int hangingID;
    int crouchID;
    int speedID;
    int fallID;

    void Start()
    {
        anim = GetComponent<Animator>();//获得Animator
        movement = GetComponentInParent<PlayerMovement>();//获得PlayerMovement
        rb = GetComponentInParent<Rigidbody2D>();//获得Robbie的RigidBody

        groundID = Animator.StringToHash("isOnGround");//将字符型转换为整数型
        hangingID = Animator.StringToHash("isHanging");
        crouchID = Animator.StringToHash("isCrouching");
        speedID = Animator.StringToHash("speed");
        fallID = Animator.StringToHash("veticalVelocity");
    }

    // Update is called once per frame
    void Update()
    {
        anim.SetFloat(speedID, Mathf.Abs(movement.xVelocity));
        anim.SetBool("isOnGround", movement.isOnGround);//因为有时用字符型传递会出问题,推荐用编号,,因为传递字符型有时候会出现一些问题
        //anim.SetBool(groundID, movement.isOnGround);
        anim.SetBool(hangingID, movement.isHanging);
        anim.SetBool(crouchID, movement.isCrouch);
        anim.SetFloat(fallID, rb.velocity.y);


    }

   

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值