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

Dylan Teixeira
Dylan Teixeira
4,284 Points

Frog won't turn

Why won't the frog turn? Only moves forward.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour {

private Animator playerAnimator;
private float moveHorizantal;
private float moveVertical;
private Vector3 movement;
private float turningspeed = 20f;
private Rigidbody playerRigidbody;
private float turningSpeed;

// Use this for initialization
void Start () {
    // Gather Components from the Player GameObject
    playerAnimator = GetComponent<Animator> ();
    playerRigidbody = GetComponent<Rigidbody> ();

}

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

    // Gather Input from the Keyboard
    moveHorizantal = Input.GetAxisRaw ("Horizontal");
    moveVertical = Input.GetAxisRaw ("Vertical");

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

}

void FixedUpdate () {

    // If the Player's Movement Vector does not Equal Zero... 
    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 new incremental rotation
        playerRigidbody.MoveRotation(newRotation);  

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

    }
}

}

1 Answer

Hi Dylan,

You've assigned turningspeed as 20f, then initialized turningSpeed without assigning a value to it. turningSpeed is used in your Lerp in FixedUpdate, so you'd either want to use turningspeed instead or assign a value to turningSpeed.

Hope that helps!

Dylan Teixeira
Dylan Teixeira
4,284 Points

Thanks so much I fixed it! All i had to do was go to turningspeed in my lurp and make the S lowercase.