Raghunathen S
Raghunathen S

Reputation: 37

how to stop a particular animation in animator unity?

I want to play a particular animation in loop when a key is pressed and want it to stop when the same key is released. How to do it? It is Unity2D Code of the character i wanna animate and move at the same time. Don't Troll I am a 14 year old Newbie. My Code Below Does not does the Job. //Something to skip minimum words of StackOverflow asda dfasdgaga asfgafa gsghagaggasgagasdgasgasdgagasggjdfhjdfgnudy

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


public class playermovement : MonoBehaviour
{
    // Animator
    Animator m_Animator;
    public float runSpeed = 10f;
    public float Jumpspeed = 50f;

    void Start()
    {
        m_Animator = gameObject.GetComponent<Animator>();
    }

    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {

        // THE Shortcut SHIT
        KeyCode RightKey = KeyCode.D; //shortcut
        KeyCode LeftKey = KeyCode.A; //shortcut
        KeyCode jump = KeyCode.Space; //shortcut


        // BUTTON PRESSED AND ANIMATION Bitch
        if (Input.GetKey(RightKey)) //when Right Key pressed (D)
        {

            Right();


        }
        else if (Input.GetKey(LeftKey)) //when Left Key pressed (A)
        {

            Left();


        }
        else if (Input.GetKey(jump)) //when (space) JUMP is pressed
        {

            Jump();


        }

    }


    // THE MOVEMENTS SHIT
    public void Right()
    {
        KeyCode RightKey = KeyCode.D; //shortcut        
        GetComponent<Rigidbody2D>().AddForce(Vector2.right * runSpeed);
        if (Input.GetKeyDown(RightKey))
        {
            m_Animator.Play("Rightanim");
        }
        else
        {
            m_Animator.PlayInFixedTime("Rightanim", 1, 0.0f);
        }



    }
    public void Left()
    {
        GetComponent<Rigidbody2D>().AddForce(Vector2.left * runSpeed);
        m_Animator.Play("leftanim");

    }
    public void Jump()
    {
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * Jumpspeed);
        m_Animator.Play("jumpanim");

    }







}

Upvotes: 0

Views: 1243

Answers (1)

Akin Erkan
Akin Erkan

Reputation: 321

First of all define an another rigidbody component for your playermovement to avoid GetComponent().AddForce(Vector2.up * Jumpspeed); in every key press GetComponent() has a cost in update. define a rigidbody2D _rb; and get in in

start() 
{ 
 _rb = GetComponent<Rigidbody2D>()
}

For your animation, you can define Float paremeters in animator (Window > animation > animator) for every movement key like RightSpeed, LeftSpeed.

Then add transiton condition to right anim and trasition back previous anim with float RightSpeed value for this Select condition line and add RightSpeed parameter. Select greater and make it 0.1. and turning back condition is less then 1.

Change

if (Input.GetKeyDown(RightKey))
        {
            m_Animator.setfloat("RightSpeed", 1f);
        }
// and add an another if check

if (Input.GetKeyUp(RightKey)) // this get key up trigger when user stop holding key
        {
            m_Animator.setfloat("RightSpeed", 0f);
        }

Upvotes: 1

Related Questions