Search Unity

Need help triggering player animation with a collider

Discussion in 'Animation' started by matthew77, Dec 7, 2017.

  1. matthew77

    matthew77

    Joined:
    Dec 17, 2016
    Posts:
    29
    I am trying to use a collider trigger to trigger a player animation using mecanim.

    I am using this script:
    Code (csharp):
    1.  #pragma strict
    2.  
    3. function OnTriggerEnter (collision : Collider) {
    4.  
    5. if (collision.gameObject.tag == "Player"){
    6.  
    7. collision.gameObject.GetComponent.().Play("bench sit new");
    8.  
    9. }
    10.  
    11. }
    I get the error "animation state could not be played because animation could not be found".

    However I can access the animation with a key press using the following code:
    Code (csharp):
    1.  using System.Collections; using System.Collections.Generic; using UnityEngine;
    2.  
    3. public class animation : MonoBehaviour {
    4.  
    5. [LIST=1]
    6. [*]public Animator anim;
    7. [*]// Use this for initialization
    8. [*]void Start () {
    9. [*]     anim = GetComponent<Animator>();
    10. [*]}
    11. [*]// Update is called once per frame
    12. [*]void Update () {
    13. [*]     if (Input.GetKeyDown("d"))
    14. [*]     {
    15. [*]         anim.Play("drunk");
    16. [*]     }
    17. [*]     if (Input.GetKeyDown("a"))
    18. [*]     {
    19. [*]         anim.Play("anim_runa_run_left");
    20. [*]     }
    21. [*]}
    22. [*]private void FixedUpdate()
    23. [*]{
    24. [*]     if (Input.GetKeyUp("7"))
    25. [*]     {
    26. [*]         anim.Play("easy_FuncyBasic");
    27. [*]     }
    28. [*]     if (Input.GetKeyUp("2"))
    29. [*]     {
    30. [*]         anim.Play("breakdance");
    31. [*]     }
    32. [*]     if (Input.GetKeyUp("3"))
    33. [*]     {
    34. [*]         anim.Play("hard_BounceStep");
    35. [*]     }
    36. [*]     if (Input.GetKeyUp("4"))
    37. [*]     {
    38. [*]         anim.Play("normal_HipCircle");
    39. [*]     }
    40. [*]     if (Input.GetKeyUp("w"))
    41. [*]     {
    42. [*]         anim.Play("Grounded");
    43. [*]     }
    44. [*]     if (Input.GetKeyUp("u"))
    45. [*]     {
    46. [*]         anim.Play("upset");
    47. [*]     }
    48. [*]     if (Input.GetKeyUp("5"))
    49. [*]     {
    50. [*]         anim.Play("hard_BasicTurn");
    51. [*]     }
    52. [*]     if (Input.GetKeyUp("6"))
    53. [*]     {
    54. [*]         anim.Play("dance new");
    55. [*]    
    56. [*]     }
    57. [*]     if (Input.GetKeyUp("e"))
    58. [*]     {
    59. [*]         anim.Play("eat2");
    60. [*]     }
    61. [*]     if (Input.GetKeyUp("l"))
    62. [*]     {
    63. [*]         anim.Play("laugh");
    64. [*]     }
    65. [*]     if (Input.GetKeyUp("b") )
    66. [*]     {
    67. [*]         anim.Play("backflip");
    68. [*]     }
    69. [*]     if (Input.GetKeyUp("g"))
    70. [*]     {
    71. [*]         anim.Play("sitting");
    72. [*]     }
    73. [*]     if (Input.GetKeyUp("1"))
    74. [*]     {
    75. [*]         anim.Play("bench sit new");
    76. [*]     }
    77. [*]     if (Input.GetKeyUp("9"))
    78. [*]     {
    79. [*]         anim.Play("lay down");
    80. [*]     }
    81. [*]     if (Input.GetKeyUp("d"))
    82. [*]     {
    83. [*]         anim.Play("drinking");
    84. [*]     }
    85. [*]     if (Input.GetKeyUp("8"))
    86. [*]     {
    87. [*]         anim.Play("waterslide");
    88. [*]     }
    89. [*]     if (Input.GetKeyUp("s"))
    90. [*]     {
    91. [*]         anim.Play("laydown");
    92. [*]     }
    93. [*]     if (Input.GetKeyUp("a"))
    94. [*]     {
    95. [*]         anim.Play("anim_runa_idle_left");
    96. [*]    
    97. [*]     }
    98. [*]}
    99. [/LIST]
    100. }
    I am using mecanim and my animations are marked "humanoid". They play with the key press script without issue but within my project I also need collider triggers for some animations. I've searched high and low and haven't found any answers.

    I've seen some suggest marking the animations as "legacy" but I think that's old advice for the legacy animation system and the animations won't play when marked that way.

    I'm really desperate for help on this as it's very important to my project. Please help.

    Thanks in advance for any help you can give me.