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

Ayush Bhargava
Ayush Bhargava
791 Points

frog won't move forward

My frog has a problem of moving forward at the ned of this video There is a yellow error which says the refrenced script on there behavior is missing. This is my code

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 ("Horizanatal");
        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);
                }
    }

}

4 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Ayush

It looks like a simple typo to me, try looking at the spelling of Horizontal in your update() method as per the below

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);
                }
    }

}

I've not tested the update, let me know if it doesn't work

Daniel

Daniel Hartin
Daniel Hartin
18,106 Points

Ok sorry that didn't work I'm unsure how far you have got in the videos but the lines of your code do match what I've got in my scripts (below), I have completed the game development course though so it may include sections you haven't covered yet;

Sorry I can't be of more help but I'm still a novice with Unity myself, by all means create a new script and play around with the code below to see if you can get it to work in your game

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    private Animator playerAnimator;
    private float moveHorizontal;
    private float moveVertical;
    private Vector3 movement;
    private float turningSpeed = 20f;
    private Rigidbody playerRigidBody;
    [SerializeField]
    private RandomSoundPlayer randomSoundPlayer;

    // Use this for initialization
    void Start () {

        playerAnimator = GetComponent<Animator>();
        playerRigidBody = GetComponent<Rigidbody>();

    }

    // 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(){

        //Fixed update runs after Update if computing available, helps to speed up animations

        if(movement != Vector3.zero){ //Change speed to 3 if the user presses a key

            //Quaternions are used to calculate X,Y,Z rotations in unity instead of altering the values manually
            Quaternion targetRotation = Quaternion.LookRotation(movement,Vector3.up);
            //newRoation.lerp is used to smooth out the rotation transistion by time calculated by math
            Quaternion newRotation = Quaternion.Lerp(playerRigidBody.rotation,targetRotation,turningSpeed * Time.deltaTime);

            playerRigidBody.MoveRotation(newRotation);
            playerAnimator.SetFloat("Speed",3f);

            randomSoundPlayer.enabled = true;


        }else{

            playerAnimator.SetFloat("Speed",0f);
            randomSoundPlayer.enabled = false;

        }

    }
}

Daniel

Ayush Bhargava
Ayush Bhargava
791 Points

Thanks,Daniel It fixed my other small errors but the main errors as i said above is still a problem "the refrenced script on this behavior is missing"

hi, on the Macintosh version of Unity, the PlayerMovement script is added automatically to the inspector as show on the video at minute 11:47. On the Windows, I had to drag the PlayerMovement script to the Inspector to make the Frog jump. Hope this helps.

I have the same issue, Any solution?

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);
            }
}

}

viktor Degerman
viktor Degerman
3,056 Points

you need one more } playerAnimator.SetFloat ("Speed", 0f); } } }