Search Unity

How to stop triggered animation in unity?

Discussion in 'Animation' started by Shayalp1, Oct 11, 2020.

  1. Shayalp1

    Shayalp1

    Joined:
    Oct 10, 2020
    Posts:
    2
    I'm actually trying to create a game in which the player is set to idle animation by default, so upon trigger when the hook grabs the jewel the rope wrap animation starts but I cant seem to be able to stop the rope wrap animation and go back to idle. Can anyone assist me with this?

    This is the code for the Player Animation Script

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerAnimation : MonoBehaviour
    7. {
    8.     private Animator anim;
    9.  
    10.     void Awake()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.     }
    14.  
    15.     public void IdleAnimation()
    16.     {
    17.         anim.SetTrigger("Idle");
    18.     }
    19.  
    20.     public void PullingItemAnimation()
    21.     {
    22.         anim.SetTrigger("ropeWrap");
    23.     }
    24.  
    25. }

    This is the code for the Hook Script

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class HookScripts : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private Transform itemHolder;
    10.  
    11.     private bool itemAttached;
    12.  
    13.     private HookMovement hookMovement;
    14.  
    15.     private PlayerAnimation playerAnim;
    16.  
    17.     void Awake() {
    18.         hookMovement = GetComponentInParent<HookMovement>();
    19.         playerAnim = GetComponentInParent<PlayerAnimation>();
    20.     }
    21.  
    22.     void OnTriggerEnter2D(Collider2D target) {
    23.  
    24.         if (!itemAttached && (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
    25.         || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
    26.         || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL
    27.         || target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE)){
    28.  
    29.             itemAttached = true;
    30.  
    31.             target.transform.parent = itemHolder;
    32.             target.transform.position = itemHolder.position;
    33.             // Set the position of the hook to the position of the item
    34.  
    35.             hookMovement.move_Speed = target.GetComponent<ItemScripts>().hook_Speed;
    36.  
    37.             hookMovement.HookAttachedItem();
    38.  
    39.             //animate player
    40.             playerAnim.PullingItemAnimation();
    41.  
    42.  
    43.             if (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
    44.                 || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
    45.                 || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL)
    46.             {
    47.  
    48.                 SoundManager.instance.HookGrab_Jewel();
    49.             }
    50.             else if (target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE){
    51.  
    52.                 SoundManager.instance.HookGrab_Stone();
    53.             }
    54.  
    55.                 SoundManager.instance.WinchCrank(true);
    56.  
    57.         } // If the target is an item
    58.  
    59.         if (target.tag == Tags.DELIVER_ITEM){
    60.  
    61.             if(itemAttached){
    62.  
    63.                 itemAttached = false;
    64.                 Transform objChild = itemHolder.GetChild(0);
    65.                 objChild.parent = null;
    66.                 objChild.gameObject.SetActive(false);
    67.  
    68.                 playerAnim.IdleAnimation();
    69.                 SoundManager.instance.WinchCrank(false);
    70.             }
    71.         }// Remove items after winching
    72.  
    73.     }// On trigger enter
    74. }
    75.  
     
  2. BojjanDev

    BojjanDev

    Joined:
    Jul 11, 2022
    Posts:
    1
    bro i have the same question