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

Darius Dennis
Darius Dennis
1,433 Points

Frog disappears after 4 seconds of pressing the spacebar and I can't seem to find out what's wrong

Frog just disappears after 4 seconds

we have nothing to look at

Pablo Zirilli
Pablo Zirilli
Courses Plus Student 3,327 Points

Hi Darius, it can be due to many reasons, may be wrong the "movement" code, or you can have bad map "levels". You should see the code to be able to detect the error. probably have some "input key" with the spacebar or something like that.

If you want paste some code to check.

Grettings!

Darius Dennis
Darius Dennis
1,433 Points

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

public class GameState : MonoBehaviour {

private bool gameStarted = false;

[SerializeField]
private Text gameStateText;

[SerializeField]
private GameObject player;

[SerializeField]
private BirdMovement birdMovement;

[SerializeField]
private FollowCamera followCamera;

private float restartDelay = 3f;
private float restartTimer;
private PlayerMovement playerMovement;
private PlayerHealth playerHealth;

// Use this for initialization
void Start () {

    Cursor.visible = false;

    playerMovement = player.GetComponent<PlayerMovement> ();
    playerHealth = player.GetComponent<PlayerHealth> ();

    //Prevent player from moving at start of the game
    playerMovement.enabled = false;

    //Prevent bird from moving at the start of the game
    birdMovement.enabled = false;

    //Prevent the follow camera from moving to its game position
    followCamera.enabled = false;

}

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

    //If the game is not started and the player presses the space bar...
    if (gameStarted == false && Input.GetKeyUp (KeyCode.Space)) {
        StartGame();
    }

    //If the player is no longer alive...
    if(playerHealth.alive == false){
        EndGame();

        //...increment a time to count up to restarting....
        restartTimer = restartTimer + Time.deltaTime;

        //...and if it reaches the restart delay...
        if(restartTimer >= restartDelay){

            //...then reload the currently loaded scene
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

        }
    }
}

private void StartGame() {
    //Set the game state
    gameStarted = true;

    //Remove the start text
    gameStateText.color = Color.clear;

    //Allow player movement
    playerMovement.enabled = true;

    //Allow bird movement
    birdMovement.enabled = true;

    //Allow the camera to move
    followCamera.enabled = true;
}

private void EndGame() {

    //Set the game state
    gameStarted = false;

    //Show the game over text
    gameStateText.color = Color.white;
    gameStateText.text = "Game Over!";

    //Remove the player from the game
    player.SetActive (false);
}
Darius Dennis
Darius Dennis
1,433 Points

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

public class PlayerMovement : MonoBehaviour {

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

// 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 () {

    //gathering input from Keyboard
    moveHorizonatal = Input.GetAxisRaw ("Horizontal");
    moveVertical = Input.GetAxisRaw ("Vertical");

    movement = new Vector3 (moveHorizonatal, 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 movement Vector...
        Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);

        //...and create another rotation that movesd 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 do not play jump animation
        playerAnimator.SetFloat ("Speed", 0f);
    }

}