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

Can't call animation through script

Discussion in 'Scripting' started by Code1345, Aug 13, 2017.

  1. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Essentially, I am making a script that calls an animation whenever you are within certain distance of an object and hit a key. The game tells how close/far you are from the object using a raycast, which works fine and changes whenever you move. But then I get close to the door and hit E, nothing happens. I did a debug.log and the animation isn't even called. I'd love to know what I am doing wrong here. Any help is appreciated. Door animation script is below, along with the raycast.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DoorAnimation : MonoBehaviour {
    7.     public Animator anim;
    8.     public AudioSource dooropen;
    9.     float TheDistance = PlayerRaycast.DistanceFromTarget;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         anim = GetComponent<Animator>();
    14.     if (Input.GetButtonDown("Action")) {
    15.         if (TheDistance <= 2) {
    16.             OpenTheDoor();
    17.  
    18.         }
    19.    
    20. }
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void OpenTheDoor () {  
    26. anim.GetComponent<Animator>().Play("Door001Anim", -1, 0);
    27.         if(Input.GetButtonDown("Action"))
    28.         { dooropen.Play();
    29.          }
    30.  
    31.     }
    32. }
    33.    
    34.  


    (Raycast script)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PlayerRaycast: MonoBehaviour {
    6. public static float DistanceFromTarget;
    7.  
    8. public float ToTarget;
    9.  
    10.  
    11. void  Update (){
    12.     RaycastHit hit;
    13.         if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out hit)) {
    14.             ToTarget = hit.distance;
    15.             DistanceFromTarget = ToTarget;      
    16.         }
    17. }
    18.  
    19. }
    20.  
     
  2. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    You're retrieving the Animator component in your Start method, so there's no need to call
    Code (CSharp):
    1. anim.GetComponent<Animator>()
    in your OpenTheDoor method. I would also use
    Code (CSharp):
    1. Input.GetButton("Action")
    instead.

    Try:
    Code (CSharp):
    1. void OpenTheDoor() {
    2.     anim.Play("Door001Anim", -1, 0);
    3.     if (Input.GetButton("Action")) {
    4.         dooropen.Play();
    5.     }
    6. }
     
    Code1345 likes this.