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

Bridie Begbie
Bridie Begbie
785 Points

Player Rotation - Frog game

Ive got the to the point where the frog shold change direction with keyboard inputs in play mode but it doesn't. Can't see any typos in code but there must be!

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

}

2 Answers

Derek Briggs
Derek Briggs
2,817 Points

Have you found the solution yet? Because i looked for 5 full minutes and checked this against my working code. You seem to have no typos. Was the Frog moving at all? Did you have the animation set properly?

Bridie Begbie
Bridie Begbie
785 Points

Thanks so much for checking. Yes ive got it to work now. I think the issue was with it not saving properly in unity so had to attach a new cut and paste script and then it worked!