如何统一停止触发的动画?

问题描述

我实际上是在尝试创建一个认情况下将玩家设置为空闲动画的游戏,因此当钩子抓住宝石时触发时,绳子缠绕动画便开始了,但我似乎无法停止绳子缠绕动画,然后回到闲置状态。钩子也抓住了一件以上的物品,有人可以帮助我吗?这是动画的视觉插图

Idle Animation

Rope Wrap - The player starts winching in the jewel

以下是Hook脚本的代码

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

public class HookScripts : MonoBehavIoUr {

[Serializefield]
private Transform itemHolder;

private bool itemAttached;

private HookMovement hookMovement;

private PlayerAnimation playerAnim;

void Awake() {
    hookMovement = GetComponentInParent<HookMovement>();
    playerAnim = GetComponentInParent<PlayerAnimation>();
}

void OnTriggerEnter2D(Collider2D target) {

    if (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM 
        || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM 
        || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL 
        || target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE){

        itemAttached = true;

        target.transform.parent = itemHolder;
        target.transform.position = itemHolder.position;
        // Set the position of the hook to the position of the item

        hookMovement.move_Speed = target.GetComponent<ItemScripts>().hook_Speed;

        hookMovement.HookAttachedItem();

        //animate player
        playerAnim.PullingItemAnimation();


        if (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
            || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
            || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL)
        {

            SoundManager.instance.HookGrab_Jewel();
        }
        else if (target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE){

            SoundManager.instance.HookGrab_Stone();
        }

            SoundManager.instance.WinchCrank(true);

    } // If the target is an item

    if (target.tag == Tags.DELIVER_ITEM){

        if(itemAttached){

            itemAttached = false;
            Transform objChild = itemHolder.GetChild(0);
            objChild.parent = null;
            objChild.gameObject.SetActive(false);

            playerAnim.IdleAnimation();
            SoundManager.instance.WinchCrank(false);

        }
    }// Remove items after winching

}// On trigger enter

}

这是播放器动画脚本的代码

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

public class PlayerAnimation : MonoBehavIoUr
{
    private Animator anim;

    void Awake()
    {
        anim = GetComponent<Animator>();
    }

    public void IdleAnimation()
    {
        anim.SetTrigger("Idle");
    }

    public void PullingItemAnimation()
    {
        anim.SetTrigger("ropeWrap");
    }

}

解决方法

我从不擅长Unity中的动画,但是我可以帮助解决多项问题。当前,当钩子碰到一个物品时,您所要检查的只是该物品是什么。 itemAttached变量已设置,但从未真正使用过。您需要添加一些支票,上面写着:“如果我已经有一个物品,请不要再拿起另一个。实际上,只需在条件集合中添加一个和即可:

if (!itemAttached && (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM 
        || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM 
        || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL 
        || target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE)){
    //attach the item
}