Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Player Input and Cameras Move the Player with Animation

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Animator does not exist, Please help!

I mean the Animator in the variable, it doesn't exist, that why the frog does't move. And also the color of this is black in the Mono develop.

Code

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    private Animator playerAnimator;
    private float moveHorizontal;
    private float moveVertical;
    private Vector3 movement;
    private float turingSpeed = 20f;
    private Rigidbody playerRigidbody;


    // Use this for initialization
    void Start () {

        // Gather the Animator component from the Player GameObject
        playerAnimator = GetComponent<Animator> ();
        playerRigidbody = GetComponent<Rigidbody> ();

    }

    // Update is called once per frame
    void Update () {

        // Gather input from the keyboard
        moveHorizontal = Input.GetAxisRaw ("Horizontal");
        moveVertical = Input.GetAxisRaw ("Vertical");

        movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    }

    void FixedUpdate () {

        // If the player's movement doesnot equal zero...
        if (movement != Vector3.zero) {
            // ...then player the jump animation.
            playerAnimator.SetFloat ("speed", 3f);
        } else {
            // Otherwise don't player the jump animation.
            playerAnimator.SetFloat ("speed", 0f);
        }

    }

}
Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

thanks for that answer, but i already done that and still not exist.

Nick Pettit
Nick Pettit
Treehouse Teacher

Are you getting any errors in the console? You can view console errors in the Unity editor by going to the Window menu at the top of the screen and choosing Console.

Actually I think it's because in your fixedUpdate...

playerAnimator.SetFloat("speed", 3f);

...you use lower case "speed", and it's supposed to be capitalized like "Speed".

To test this theory, I just replaced "Speed" with "speed" in my own game and the frog wouldn't move.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Nothing error just the warning, but when I change speed to Speed it work well. But still that when move the mouse over it said http://imgur.com/Hv8j4DH. Thank you for all answer.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

I confused that what part of this code do. When press WSAD and the frog move. We didn't specific that WASD is the keys right?

Nothing error just the warning, but when I change speed to Speed it work well. But still that when move the mouse over it said http://imgur.com/Hv8j4DH. Thank you for all answer.

What editor are you using? That Animator is the Animator class, which definitely exists. It seems your editor just doesn't know about it for some reason.

I confused that what part of this code do. When press WSAD and the frog move. We didn't specific that WASD is the keys right?

In one of the videos, he talks about how Input.GetAxisRaw gives you the player's keyboard input -- and that Unity handles the WASD and Up/Down/Left/Right capturing for you.

1 Answer

In your PlayerMovement.cs script, you should have the following lines:

At the top of the PlayerMovement class, declare the Animator:

private Animator playerAnimator;

In the Start method, initialize the Animator using GetComponent:

playerAnimator = GetComponent<Animator>();

In the FixedUpdate method, you should be interacting with the playerAnimator variable:

playerAnimator.SetFloat("Speed", 3f);