Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question about triggering an animation (Unity 5.0)

Discussion in 'Scripting' started by Fyurien, Aug 16, 2015.

  1. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Hello,

    This may be the simplest question but I can't seem to trigger an animation using JS in Unity 5.0. All of the examples online are for deprecated code, and so don't seem to work with this version of Unity. Here is the code...

    Code (CSharp):
    1. function Update (){
    2.     if(open){
    3.     //Open door
    4.  
    5.     //play animation
    6.  
    7.     }
    8.    
    9.     else
    10.     {
    11.     //Close door
    12.  
    13.     //do nothing
    14.    
    15.     }
    16.  
    17.     if(Input.GetKeyDown("4") && enter)
    18.     {
    19.    
    20.     open = !open;
    21.    
    22.     }
    23. }
    Now the animation is called openDoorNormal and is attached to the Animation component for the object, which is a door. I've tried the following...

    Code (CSharp):
    1. door.GetComponent.<Animation>.Play("doorOpenNormal");
    But it errors out. Actually now that I think about it, here is the full code...

    Code (CSharp):
    1. private var open : boolean;
    2. private var enter : boolean;
    3. private var door : Transform;
    4.  
    5.  
    6. //Main function
    7. function Update (){
    8.     if(open){
    9.     //Open door
    10.  
    11.     door.GetComponent.<Animation>.Play("doorOpenNormal");
    12.  
    13.     }
    14.    
    15.     else
    16.     {
    17.     //Close door
    18.  
    19.     //do nothing
    20.    
    21.     }
    22.  
    23.     if(Input.GetKeyDown("4") && enter)
    24.     {
    25.    
    26.     open = !open;
    27.    
    28.     }
    29. }
    30.  
    31. //Activate the Main function when player is near the door
    32. function OnTriggerEnter (other : Collider)
    33.     {
    34.    
    35.     if (other.gameObject.tag == "Activate")
    36.     {
    37.    
    38.     enter = true;
    39.    
    40.     }
    41.     }
    42.  
    43. //Deactivate the Main function when player is go away from door
    44. function OnTriggerExit (other : Collider)
    45.     {
    46.     if (other.gameObject.tag == "Activate")
    47.     {
    48.    
    49.     enter = false;
    50.    
    51.     }
    52. }
    Thank you in advance for any suggestions..
     
  2. Fyurien

    Fyurien

    Joined:
    Aug 16, 2015
    Posts:
    20
    Ok, so I think I found part of my problem. It looks like I was combining JS and C#... oops. Here is the C# version... still getting errors, but looks better...

    Code (CSharp):
    1. // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
    2. // Do test the code! You usually need to change a few small bits.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class openDoorNormal : MonoBehaviour {
    8.     private bool  open;
    9.     private bool  enter;
    10.     public Animation anim;
    11.    
    12.    
    13.     //Main function
    14.     void  Update (){
    15.         if(open){
    16.  
    17.             anim.Play("openDoorNormal");
    18.            
    19.         }
    20.        
    21.         else
    22.         {
    23.             //Close door
    24.            
    25.             //do nothing
    26.            
    27.         }
    28.        
    29.         if(Input.GetKeyDown("4") && enter)
    30.         {
    31.            
    32.             open = !open;
    33.            
    34.         }
    35.     }
    36.    
    37.     //Activate the Main function when player is near the door
    38.     void  OnTriggerEnter ( Collider other  ){
    39.        
    40.         if (other.gameObject.tag == "Activate")
    41.         {
    42.            
    43.             enter = true;
    44.            
    45.         }
    46.     }
    47.    
    48.     //Deactivate the Main function when player is go away from door
    49.     void  OnTriggerExit ( Collider other  ){
    50.         if (other.gameObject.tag == "Activate")
    51.         {
    52.            
    53.             enter = false;
    54.            
    55.         }
    56.     }
    57. }