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 Rotate the Player Towards the Target

Frog only moves forward.

Frog will only move forward.

My code:

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

// Use this for initialization
void Start () {
    // Gathers the components from the player gameobject for loc and rot        
    playerAnimator = getComponent<Animator> ();
    player Rigidbody = getComponent<Rigidbody> ();

}

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

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

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

void FixedUpdate () {


    // If the player's movement vector does not equal 0...
    if (movement != Vector3.zero) {

        // ... then create a target rotation based on the movement vector...
        Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);

        //... and create another rotation that moves from the current rotation to the target rotation...
        Quaternion newRotation = Quaternion.Lerp (playerRigidbody.rotation, targetRotation, turningSpeed * Time.deltaTime);

        // ... and change the players rotation to the incremental rotation...
        playerRigidbody.MoveRotation(NewRotation);

        // ... then play the jump animation...
        playerAnimator.SetFloat("Speed", 3f);

    } else {

        // ...otherwise, don't play the animation.
        playerAnimator.SetFloat("Speed", 0f)

    }


}

}

Also, how did we tell the unity engine to use the WASD and arrow keys as the input for moving around? When did we tell the game how far to rotate when we press the left or right arrow keys? I am very confused.

6 Answers

Check & see if this code will help you:

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;

    // Use this for initialization
void Start () 
{

    // Gather the components 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 players movement vector does not equal zero...
    if (movement != Vector3.zero) { 

        // ...then create a target rotation on the movement vector...
        Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);

        //...and create another rotation that fills anim gap between current rotation and target rotation so less jumpy
        Quaternion newRotation = Quaternion.Lerp (playerRigidbody.rotation, targetRotation, turningSpeed * Time.deltaTime);

        //...and change the players rotation to the new incremental rotation
        playerRigidbody.MoveRotation(newRotation);

        // ...then play the jump animation.
        playerAnimator.SetFloat ("Speed", 3f);
    } else {
        // otherwise don't play the jump animation.
        playerAnimator.SetFloat ("Speed", 0f);
    }
}

Doesn't work. Still only moves forward.

I have been stuck for a few days. Does anyone know the answer?

I think you guys are missing a ending curly brace for the class.

Wouldn't that result in a compiler error? I am not getting one.

it seems every codes are alright except this (( player Rigidbody = getComponent<Rigidbody> (); )) because space in playerRigidbody variable. also make sure that the playermovement script is checked in player inspector, mainly some small mistakes make problems :)

Still only moves forward.

Try walking towards the stones and then try to turn again. It's possible you connected your camera to the frog instead of the environment so that the camera moves and rotates when the frog rotates/moves. I had the same thing, it'll become clear once you reach the stones. If this is the case: drag the camera from Player to Environment.

P.s. I said drag it to 'Environment' but I guess Camera is an entity on itself, so drag it to the parent category (the name of your scene)