【问题标题】:How do I delay an animation using my character movement script?如何使用我的角色移动脚本延迟动画?
【发布时间】:2017-12-08 10:58:14
【问题描述】:

下面是我的玩家移动脚本的副本,其中包含动画我的角色左右移动、跳跃和射击他的弓的功能。我还能够让我的角色从射弓过渡到处于警觉动画中,但我想知道如何编写代码,以便在我射弓后 5 秒内我会保持警觉,然后再回到我的默认“空闲”动画。

我还希望我的角色能够在保持警觉的同时四处走动(即将警报动画转换为步行动画),但如果他停止移动,他就会恢复警觉。目前,如果我的角色在他的警戒姿态中行走,他将回到空闲状态。下面的脚本,特别是第 98-102 行(我的“playerAlert”函数)提出了另一个问题,即我的角色在此函数发生后无法执行他的“射击”动画,但我不知道/无法理解如何编写我上面提到的代码。

我正在使用 Unity 5.6。

感谢你们能给我的任何帮助。

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

     public class player_1_controller : MonoBehaviour {

         Rigidbody2D myRB; 
         Animator myAnim;
         bool facingRight;

         //movement variables
         public float maxSpeed;

         //jumping variables
         bool grounded = false;
         float groundCheckRadius = 0.2f;
         public LayerMask groundLayer;
         public Transform groundCheck;
         public float jumpHeight;

         //arrow shooting variables
         bool firing = true;
         public float arrowShoot;
         public Transform arrowTip;
         public GameObject arrow;
         public float fireRate = 0.5f;
         public float nextFire = 0f;

         // Use this for initialization
         void Start () {

             myRB = GetComponent<Rigidbody2D> ();
             myAnim = GetComponent<Animator> ();
             facingRight = true;
             }

         // Update is called once per frame
         void Update () {

             //player jump
             if (grounded && Input.GetButtonDown ("Jump")) {
                 grounded = false;
                 myAnim.SetBool ("isGrounded", grounded);
                 myRB.AddForce (new Vector2 (0, jumpHeight));
             }

             //player shooting
             if (grounded && Input.GetButtonDown ("Fire1")) {
                 myAnim.SetBool ("arrowShoot", firing);
                 Invoke ("fireArrow", 1);
             }

         }

             void FixedUpdate() {

             //player movement
             float move = Input.GetAxis ("Horizontal");
             myAnim.SetFloat ("speed", Mathf.Abs (move));

             myRB.velocity = new Vector2 (move * maxSpeed, myRB.velocity.y);

             if (move > 0 && !facingRight) {
                 flip ();
             } else if (move < 0 && facingRight) {
                 flip ();
             }

             //player jump; check if we are grounded - if not, then we are falling
             grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
             myAnim.SetBool ("isGrounded", grounded);

             myAnim.SetFloat ("verticalSpeed", myRB.velocity.y);
         }

             void flip () {
             facingRight = !facingRight;
             Vector3 theScale = transform.localScale;
             theScale.x *= -1;
             transform.localScale = theScale;

         }

         void fireArrow() {

             if (Time.time > nextFire) {
                 nextFire = Time.time + fireRate;
                 if (facingRight) {
                     Instantiate (arrow, arrowTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
                 } else if (!facingRight) {
                     Instantiate (arrow, arrowTip.position, Quaternion.Euler (new Vector3 (0, 0, 180)));
                 }
             }
             playerAlert ();

           }

         void playerAlert () {

                 firing = false;
                 myAnim.SetBool ("arrowShoot", firing);
         }
     }

【问题讨论】:

    标签: c# animation unity3d


    【解决方案1】:

    Invoke 可以在这里为您提供帮助。摘录:

    void Start()
    {
        Invoke("LaunchProjectile", 2);
    }
    
    void LaunchProjectile()
    {
        Rigidbody instance = Instantiate(projectile);
        instance.velocity = Random.insideUnitSphere * 5;
    }
    

    您可以使用它在 5 秒后调用(调用)“返回空闲”功能。

    【讨论】:

      【解决方案2】:

      您是否考虑过在动画师内部实现它?

      您可以创建从警报动画到空闲动画的过渡,固定退出时间为 5 秒。

      这样,在 Alert 动画 5 秒后,它会过渡到 Idle 动画。

      【讨论】:

      • 这部分工作,非常感谢您的帮助。任何想法我如何能够做我上面提到的事情:“我还希望我的角色能够在保持警觉的同时四处走动(即将警报动画转换为步行动画),但如果他停止移动,他会返回警戒。目前我的角色如果在警戒姿态中行走,他会回到空闲状态。”
      • 您必须向动画师添加一个参数才能知道角色是否已做好战斗准备。然后使用该参数在参数为 false 时转换为 Idle 状态,并在参数为 true 时转换为 Alert 状态。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多