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

problem with animation

Discussion in 'Scripting' started by Rafa19961, May 18, 2016.

  1. Rafa19961

    Rafa19961

    Joined:
    May 25, 2014
    Posts:
    3
    I have this code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class InvestigateObjectScript : MonoBehaviour
    5. {
    6.     public float time;
    7.     private Animation anim;
    8.  
    9.     void Awake()
    10.     {
    11.         anim = GetComponent<Animation>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.  
    17.         if (Input.GetKey(KeyCode.S) && time <= 1.048052f)
    18.         {
    19.             anim.enabled = true;
    20.             anim["basicRigAction_002"].speed = 0.1f;
    21.             anim["basicRigAction_002"].time = time;
    22.             anim.Play("basicRigAction_002");
    23.  
    24.         }
    25.         if (Input.GetKey(KeyCode.H))
    26.         {
    27.             anim.enabled = true;
    28.             anim["basicRigAction_002"].speed = -0.1f;
    29.             anim["basicRigAction_002"].time = time;
    30.             anim.Play("basicRigAction_002");
    31.         }
    32.  
    33.         if (time <= 1.048052f && anim.enabled)
    34.         {
    35.             time = anim["basicRigAction_002"].time;
    36.         }
    37.  
    38.  
    39.        /* if (!(Input.GetKey(KeyCode.H) || Input.GetKey(KeyCode.S)))
    40.        {
    41.             anim.enabled = false;
    42.            // time = anim["basicRigAction_002"].time;
    43.         }*/
    44.            
    45.         if (Input.GetKey(KeyCode.H) && time >= 1.04053f)
    46.         {
    47.             time = 1.048052f;
    48.         }
    49.         Debug.Log("" + time.ToString());
    50.         Debug.Log("dsd" + anim["basicRigAction_002"].time.ToString());
    51.  
    52.  
    53.     }
    54.  
    55. }
    56.  
    And what i want is that when I push those buttons animatiions play, but i have the opposite when I push those buttons the animations dont play and when I let them go animations starts. Why is happening this?
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    i think it's because youre using Input.GetKey, so every frame the animation is started again from the beginning. try GetKeyDown
     
  3. Rafa19961

    Rafa19961

    Joined:
    May 25, 2014
    Posts:
    3
    It doesnt work either