W
W
WalloutDay2015-10-31 09:36:22
Android
WalloutDay, 2015-10-31 09:36:22

Animation transition not working on 2d platformer for android, unity 5?

Please help me solve the problem. the character walks on the buttons. But here's the problem =( Animation does not work.
anim.SetFloat ("Speed", Mathf.Abs (move));
by deleting various commands, I realized that this line is responsible for changing the animation. I inserted it into the script =>

using UnityEngine;
using System.Collections;

public class CharacterController1 : MonoBehaviour {
public Rigidbody2D rb2d;
public float speed;
public int move;
public bool groundCheck;
public bool facingRight = true;

Animator anim; 

void Start () 
{
anim = GetComponent ();
rb2d = GetComponent();
}


void FixedUpdate () 
{
if ((move < 0) && (facingRight))
{
Flip();
}

if ((move > 0) && (!facingRight))
{
Flip();
}
groundCheck = true;
}

public void Move(int InputAxis)
{
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move)); <=========
move = InputAxis;
GetComponent().velocity = new Vector2 (move * speed, GetComponent().velocity.y);

if ((move < 0) && (facingRight))
{
Flip();
}

if ((move > 0) && (!facingRight))
{
Flip();
}
}



void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}


}

but as before, with a line, without a line, the animation is not played! (
Please help me find an error ... maybe I wrote something wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ark Tarusov, 2016-12-14
@WalloutDay

Please close or delete your questions upon receipt of an answer or by the statute of limitations

Very familiar code. I advise you to revisit the tutorial from which you took this.
But on business.
You need to create a float variable Speed ​​in the animator, then in the animator set up the transition of the animation from the state of rest to the state of running. Create arrows (google) on the arrow of the transition to the running state in Conditions, add our Speed ​​​​variable and write that it should be Greater than 0.01
And on the arrow that returns from the running state to the resting state, do the same, but Speed ​​​​should be Less than 0.01
And correct in every way.
using UnityEngine;
using System.Collections;

public class CharacterController1 : MonoBehaviour
{
    public Rigidbody2D rb2d;
    public float speed;
    public int move;
    public bool facingRight = true;

    private Animator anim;

    void Start()
    {
        anim = GetComponent<Animator> ();
        rb2d = GetComponent<Rigidbody2D> ();
    }

    void FixedUpdate()
    {
        rb2d.velocity = new Vector2(move * speed, rb2d.velocity.y);
    }

    public void Update()
    {
        float move = Input.GetAxis("Horizontal");

        anim.SetFloat("Speed", Mathf.Abs(move));

        if ((move < 0) && (facingRight))
        {
            Flip();
        }

        if ((move > 0) && (!facingRight))
        {
            Flip();
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

V
Vladimir Limarchenko, 2015-11-15
@vovo801

The first thing that caught my eye should be:
anim = GetComponent ();
rb2d = GetComponent();
Second: is there an animator and animations on this object at all? Maybe the animator is not on this object, but on its child object? To check if the animator found it, you can right-click on the name of the Inspector tab and switch to Debug mode. Then all the variables of your CharacterController will be displayed, including the animator and see if Unity found it or not.
There are many more things that can affect.
And how did you understand that this line affects if the animation does not work? What other signs, errors in the console were there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question