【问题标题】:How to stop triggered animation in unity?如何统一停止触发动画?
【发布时间】:2021-01-25 16:00:40
【问题描述】:

我实际上是在尝试创建一个游戏,其中玩家默认设置为空闲动画,因此当钩子抓住宝石时触发时绳子包裹动画开始,但我似乎无法停止绳子包裹动画并回到空闲状态。钩子也能抓到一件以上的东西,有人能帮我吗?这是动画的视觉插图

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");
    }

}

【问题讨论】:

    标签: c# unity3d game-physics 2d-games


    【解决方案1】:

    我从来不擅长 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
    }
    

    【讨论】:

    • 非常感谢,绝对解决了钩子问题!
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多