• 第六天 脚本与动画系统


    一、动画系统基本概念

    1、动画的基本概念

    (1)帧

    (2) 帧动画

    (3)3D骨骼动画

     

     

     

    (4)2D骨骼动画

    2、动画融合

    3、动画状态机

     

     (1)理解动画状态机

    (2)动画状态机设计举例

    4、根骨骼动画

     

    二、2D动画实例分析

    1、准备工作

    (1)下载资源

    (2)整理资源

    (3)图片设置

    2、角色与控制的制作步骤

    (1)下载资源

    需要修改以下代码才能进行演示:

    SimpleActivatorMenu.cs

    1. using System;
    2. using UnityEngine;
    3. #pragma warning disable 618
    4. namespace UnityStandardAssets.Utility
    5. {
    6. public class SimpleActivatorMenu : MonoBehaviour
    7. {
    8. // An incredibly simple menu which, when given references
    9. // to gameobjects in the scene
    10. //public GUIText camSwitchButton;
    11. public GameObject[] objects;
    12. private int m_CurrentActiveObject;
    13. private void OnEnable()
    14. {
    15. // active object starts from first in array
    16. m_CurrentActiveObject = 0;
    17. // camSwitchButton.text = objects[m_CurrentActiveObject].name;
    18. }
    19. public void NextCamera()
    20. {
    21. int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1;
    22. for (int i = 0; i < objects.Length; i++)
    23. {
    24. objects[i].SetActive(i == nextactiveobject);
    25. }
    26. m_CurrentActiveObject = nextactiveobject;
    27. // camSwitchButton.text = objects[m_CurrentActiveObject].name;
    28. }
    29. }
    30. }

     PlatformerCharacter2D.cs

    1. using System;
    2. using UnityEngine;
    3. #pragma warning disable 649
    4. namespace UnityStandardAssets._2D
    5. {
    6. public class PlatformerCharacter2D : MonoBehaviour
    7. {
    8. [SerializeField] private float m_MaxSpeed = 10f; // The fastest the player can travel in the x axis.
    9. [SerializeField] private float m_JumpForce = 400f; // Amount of force added when the player jumps.
    10. [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
    11. [SerializeField] private bool m_AirControl = false; // Whether or not a player can steer while jumping;
    12. [SerializeField] private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
    13. private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
    14. const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    15. private bool m_Grounded; // Whether or not the player is grounded.
    16. private Transform m_CeilingCheck; // A position marking where to check for ceilings
    17. const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
    18. private Animator m_Anim; // Reference to the player's animator component.
    19. private Rigidbody2D m_Rigidbody2D;
    20. private bool m_FacingRight = true; // For determining which way the player is currently facing.
    21. public bool m_JumpReset = false;
    22. private void Awake()
    23. {
    24. // Setting up references.
    25. m_GroundCheck = transform.Find("GroundCheck");
    26. m_CeilingCheck = transform.Find("CeilingCheck");
    27. m_Anim = GetComponent<Animator>();
    28. m_Rigidbody2D = GetComponent<Rigidbody2D>();
    29. }
    30. private void FixedUpdate()
    31. {
    32. m_Grounded = false;
    33. // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    34. // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    35. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    36. for (int i = 0; i < colliders.Length; i++)
    37. {
    38. if (colliders[i].gameObject != gameObject)
    39. {
    40. if (colliders[i].gameObject.tag == "JumpPoint")
    41. {
    42. m_JumpReset = true;
    43. Destroy(colliders[i].gameObject);
    44. }
    45. else
    46. {
    47. m_Grounded = true;
    48. }
    49. }
    50. }
    51. m_Anim.SetBool("Ground", m_Grounded);
    52. // Set the vertical animation
    53. m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
    54. }
    55. public void Move(float move, bool crouch, bool jump)
    56. {
    57. // If crouching, check to see if the character can stand up
    58. if (!crouch && m_Anim.GetBool("Crouch"))
    59. {
    60. // If the character has a ceiling preventing them from standing up, keep them crouching
    61. if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    62. {
    63. crouch = true;
    64. }
    65. }
    66. // Set whether or not the character is crouching in the animator
    67. m_Anim.SetBool("Crouch", crouch);
    68. //only control the player if grounded or airControl is turned on
    69. if (m_Grounded || m_AirControl)
    70. {
    71. // Reduce the speed if crouching by the crouchSpeed multiplier
    72. move = (crouch ? move * m_CrouchSpeed : move);
    73. // The Speed animator parameter is set to the absolute value of the horizontal input.
    74. m_Anim.SetFloat("Speed", Mathf.Abs(move));
    75. // Move the character
    76. m_Rigidbody2D.velocity = new Vector2(move * m_MaxSpeed, m_Rigidbody2D.velocity.y);
    77. // If the input is moving the player right and the player is facing left...
    78. if (move > 0 && !m_FacingRight)
    79. {
    80. // ... flip the player.
    81. Flip();
    82. }
    83. // Otherwise if the input is moving the player left and the player is facing right...
    84. else if (move < 0 && m_FacingRight)
    85. {
    86. // ... flip the player.
    87. Flip();
    88. }
    89. }
    90. // If the player should jump...
    91. if ((m_Grounded || m_JumpReset) && jump)
    92. {
    93. // Add a vertical force to the player.
    94. m_Grounded = false;
    95. m_JumpReset = false;
    96. m_Anim.SetBool("Ground", false);
    97. Vector2 resetVlocity = m_Rigidbody2D.velocity;
    98. resetVlocity.y = Mathf.Max(0, resetVlocity.y);
    99. m_Rigidbody2D.velocity = resetVlocity;
    100. m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    101. }
    102. }
    103. private void Flip()
    104. {
    105. // Switch the way the player is labelled as facing.
    106. m_FacingRight = !m_FacingRight;
    107. // Multiply the player's x local scale by -1.
    108. Vector3 theScale = transform.localScale;
    109. theScale.x *= -1;
    110. transform.localScale = theScale;
    111. }
    112. }
    113. }

    演示:

     

     

     (2)整理资源

     

     

     

     (3)图片设置

     

     

     

     

     

     

     

     

     

     

    3、动画的制作步骤

     

     

     

     

     

     

     

    4、创建动画变量

    (1)编辑动画

     

     

    注:新版本的unity没有sample可以通过拖动图片手动改变帧率。

     

     

    (2)编辑动画状态机 

     

     

     

     

    (3)创建动画变量

     

     

     

     

     

     

      5、设置动画过渡

     

     

     

     

     

     

     

    6、用脚本修改动画变量

     

     

    7、脚本编程重点提示

     

     

     

    8、总结和拓展

     

    三、三维模型与动画的导入

    1、导入示例

     

    2、三维模型资源设置

     

     

     

     

     

    3、三维动画资源设置

     

     

     

     

     

     

     

    4、模型骨骼与动画骨骼的关系

     

     

     

    四、动画进阶技术实例分析

     

    1、动画融合树

     

     

     

     

     

    2、第三人称角色控制的脚本分析

     

     

     

     

     

     

     

    3、根骨骼动画的运用

     

     

    4、动画遮罩

     

     

    5、动画层

     

    6、动画帧事件

     

     

    7、反向动力学(IK)

     

     

  • 相关阅读:
    SpringCloud系列(二)Ribbon 负载均衡的原理及详细流程
    JavaScript:js基础2
    「驱动安装」HighPoint RocketRAID R2722 磁盘阵列卡 驱动安装教程
    HTML仿腾讯微博首页(Dreamweaver网页作业)
    20230914 比赛总结
    MD5加密——原理介绍
    容器化 | 在 Kubernetes 上部署 RadonDB MySQL 集群
    黑马JVM总结(三十七)
    educoder_python:4-1-逻辑控制(if)第2关:求解一元二次方程组
    51单片机控制电动机正反转,PWM调速,记录转动圈数。
  • 原文地址:https://blog.csdn.net/qq_51701007/article/details/123730932