如何使用角色移动脚本延迟动画?

时间:2017-07-04 23:00:27

标签: c# animation unity3d

下面是我的播放器动作脚本的副本,其中包含动画我的角色左右移动,跳跃和射击他的弓的功能。我也能够让我的角色从拍摄他的弓变成警报动画,但我想知道如何编写代码以便在我拍弓后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);
         }
     }

2 个答案:

答案 0 :(得分:0)

Invoke可以在这里帮到你。摘录:

void Start()
{
    Invoke("LaunchProjectile", 2);
}

void LaunchProjectile()
{
    Rigidbody instance = Instantiate(projectile);
    instance.velocity = Random.insideUnitSphere * 5;
}

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

答案 1 :(得分:0)

您是否考虑过在动画制作中实现它?

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

这样,在进入Alert动画5秒后,它将转换为空闲动画。