Good morning everyone I am working on the “Create With Code” tutorial. I am in Challenge 3 of the
Prototype 3 This lesson is Balloons, Bombs, & Booleans Bonus 7: The Balloon can float too high
The bonus asks that
- Prevent the player from floating their balloon too high
Hint: Add a boolean to check if the balloon isLowEnough, then only allow the player to add upwards force if that boolean is true
So my thought was to create a isLowEnough variable then use this variable to check to see if its current Height of the player balloon is less than or equal to the height of the top of the Background object. I am confused on how best to reference the Background game object in my if statement on line 40 of my PlayerControllers.cs script so that I can stop the balloon from exceeding this value.
What is the best way to accomplish this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControllerX : MonoBehaviour
{
public bool gameOver;
public float floatForce;
private float gravityModifier = 2.5f;
private Rigidbody playerRb;
public ParticleSystem explosionParticle;
public ParticleSystem fireworksParticle;
public bool isLowEnough;
private float ballonHeight;
private AudioSource playerAudio;
public AudioClip moneySound;
public AudioClip explodeSound;
// Start is called before the first frame update
void Start()
{
ballonHeight = gameObject.GetComponent<>
playerRb = GetComponent<Rigidbody>();
Physics.gravity *= gravityModifier;
playerAudio = GetComponent<AudioSource>();
// Apply a small upward force at the start of the game
playerRb.AddForce(Vector3.up * 5, ForceMode.Impulse);
}
// Update is called once per frame
void Update()
{
// While space is pressed and player is low enough, float up
if (Input.GetKey(KeyCode.Space) && !gameOver && isLowEnough <= Transform.Background.position.y;
{
isLowEnough = true;
playerRb.AddForce(Vector3.up * 1 * floatForce, ForceMode.Impulse);
}
}
private void OnCollisionEnter(Collision other)
{
// if player collides with bomb, explode and set gameOver to true
if (other.gameObject.CompareTag("Bomb"))
{
explosionParticle.Play();
playerAudio.PlayOneShot(explodeSound, 1.0f);
gameOver = true;
Debug.Log("Game Over!");
Destroy(other.gameObject);
}
// if player collides with money, fireworks
else if (other.gameObject.CompareTag("Money"))
{
fireworksParticle.Play();
playerAudio.PlayOneShot(moneySound, 1.0f);
Destroy(other.gameObject);
}
}
}