Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Ashton Holgate
Ashton Holgate
6,021 Points

Frog will not move despite perfect code

I don't get it. This code seems perfect yet the frog will not move when I press the arrow keys or the WASD keys. Please can someone help!

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    private Animator playerAnimator;
    private float moveHorizontal;
    private float moveVertical;
    private Vector3 movement;

    // Use this for initialization
    void Start () {
        playerAnimator = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
        moveHorizontal = Input.GetAxisRaw("Horizontal");
        moveVertical = Input.GetAxisRaw("Vertical");

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

    void FixedUpdate() {
        if (movement != Vector3.zero) {
            playerAnimator.SetFloat("Speed", 3f);
        } else {
            playerAnimator.SetFloat("Speed", 0f);
        }
    }

}
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,845 Points

Just to add, I managed to get the frog moving myself after a bit of determination.

Would I be right in saying that you need to have the script in the player Game object folder of the prefab? :) I had all the right code but I had script files saved in various places. :)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Jonathan Grieve The problem in this case was that the Script was not connected to the Player object. Everything else was in place for the object, and the code was written, but it was never connected to the frog. :smiley:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,845 Points

Okay. :)

Will maybe rewatch the video. I was relieved to see the frog move but wasn't 100% sure what I did that accomplished it. Very exciting though. Programming a game for the first time! :)

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I've got to be honest here. I'm looking at your code and it looks fine. Are you positive the PlayerMovement script is correctly linked to the Player prefab? And just out of curiosity, have you tried the arrow keys? Mine works with both, but who knows? :smiley:

Ashton Holgate
Ashton Holgate
6,021 Points

I believe it is correct but because it has been made by Treehouse and not me I'm not entirely sure what correct would look like. I made this gif to help. Would anything else help?

https://gyazo.com/629f8d1df0f8b234a9b9ad3f3481376f

Oh and yes I've tried both the arrow keys and WASD keys.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Ashton Holgate I'm also working on this track, but am a bit further down the line than you are at the moment. Thanks for the short video. What I'm seeing in your Unity workspace when you click on the Player prefab is the Transform, Animator, RigidBody, and Box Collider sections. What I'm not seeing is the Script section. So try this... click the Player prefab. Then click add component and then in the search box at the top start typing in "PlayerMovement" . It should bring it up in a list of alternatives. Then click add component. I feel fairly certain at this point that the problem is that you've written the code, but it's not linked to the prefab. Let me know how this works out for you! :sparkles:

Ashton Holgate
Ashton Holgate
6,021 Points

Genius! It worked! Thanks so much for the help I don't think I would have worked that one out myself!