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

Question GetKeyDown pauses game instead of playing animation

Discussion in 'Scripting' started by LizzieMcGuire, Feb 25, 2023.

  1. LizzieMcGuire

    LizzieMcGuire

    Joined:
    Sep 22, 2022
    Posts:
    6
    (Not sure whether to put this in here or animation)

    I am using this code:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MilaController : MonoBehaviour
    {
    private Animator anim;
    void Start()
    {
    anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.D))
    {
    anim.SetTrigger("Dancing");
    }
    }
    }

    I have the trigger set up in the animator, on a transition that goes from Any State to Dancing. When I press D, it pauses the animation and for some reason switches the camera angle to the side, and I can't unpause the game - only get out of play mode using my mouse. It doesn't play the animation at all. I tried this using KeyCode.Q as well, and GetKey instead of GetKeyDown and it did the same thing. I am very new to unity and working through the online lessons so apologies if this is a very obvious fix.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    I'm 99% sure your issue is that you have "Can Transition To Self" checked in your transition from "Any State." Click on the transition, expand "Settings" in the inspector by clicking on the arrow, and uncheck "Can Transition To Self."
     
    LizzieMcGuire likes this.
  3. LizzieMcGuire

    LizzieMcGuire

    Joined:
    Sep 22, 2022
    Posts:
    6
    Thank you so much for responding! I unchecked it, but unfortunately nothing changed. I'm going to try to play around with the settings though - I'm hoping that it's something else in there.
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Are you getting any errors? You might have "Error Pause" enabled in the console, and that's why your game is freezing:

    Capture.JPG
     
    LizzieMcGuire likes this.
  5. LizzieMcGuire

    LizzieMcGuire

    Joined:
    Sep 22, 2022
    Posts:
    6
    That was it! As soon as I disabled "Error Pause," it worked - I did also have an error that I accidentally attached the script I was using to the plane my character was on as well, I think maybe that was messing with it. Thank you!!
     
    Unrighteouss likes this.