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

C#

My player wont move properly

My code seems to be perfect, the player, would be hopping, the animation would be going fine then seemingly randomly my player stops moving and when I try to go around the invisible barrier my player would continue moving and then in another random spot the same thing would happen again and throughout this the player's moving animation is still playing some of these invisible barriers I would be able to push through though it does significantly slow me down and this problem does not occur with the bird so it gives the player a serious disadvantage.

2 Answers

Nevermind I found the problem I just need to increase the "Y" axis so the frog sits on top of the ground and not just under it.

Thank you for taking your time to try and solve it.

Samuel Ferree
Samuel Ferree
31,722 Points

Can you post a link to your code?

This is my player movement code:

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

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 compoenents 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 player's movement Vector does not equal zero...
    if (movement != Vector3.zero) {
        // ...Then create a target rotation bases 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 player's 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);

    }
}

}