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

Bridie Begbie
Bridie Begbie
785 Points

frog rotation/jumping - cant get input keys to work after adding Quaternion code.

Assuming there must be a typo or structural error in my code because I really cant see it when I compare mine to the video tutorial! Any help greatly appreciated!

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

}

6 Answers

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

Your code looks fine, same as what I have and my project is running fine. I'd look through the project and make sure all the components are there and hooked up as needed.

Bridie Begbie
Bridie Begbie
785 Points

Thanks for checking it for me. Glad it looks right! Now to fiddle around with unity and see if i can get it working!

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

For what it's worth, try closing the program completely and reopening, that fixed a similar issue for me last night.

Bridie Begbie
Bridie Begbie
785 Points

Ha! Its like IT Crowd: "Have you tried turning it off and on again?" frustrated irish accent...

Yes have tried that. Will have another tinker. I will not be defeated!

Bridie Begbie
Bridie Begbie
785 Points

yay I got it to work. I think the issue might have been changes to the script not saving properly in Unity. Thanks so much for checking my code anyway!

Suave Przywalny
PLUS
Suave Przywalny
Courses Plus Student 5,730 Points

I had the same issue two different times. I combed through all the code, saved scene and scripts and the frog would not jump or move. The only solution to both of these instances was to restart the game development from scratch. I set up the scene and imported assets, placed them and rewrote the scripts. Now everything works. Hope this helps. Sometimes if all else fails just restart. (Only acceptable for such a small project!)