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);
}
}
}
Ok you have several problem here and getting errors on line 20 and 31.
Kill line 20, I don’t know what you intend there. Your entire Update method is a complete logic and operator mess.
Try this;
void Update()
{
// While space is pressed and player is low enough, float up
isLowEnough = (transform.Position.y < maxHeight); // Note maxHeight must be defined above
if (Input.GetKey(KeyCode.Space) && !gameOver) ;
{
if (isLowEnough)
{
playerRb.AddForce(Vector3.up * 1 * floatForce, ForceMode.Impulse);
}
}
}
Ok i updated with this code and my ballon will still exceed the height of the Background object. I am expecting that once the balloon reaches the maxHeight that the balloon would stop. so i set the maxHeight to the 9.5 value of the transform position of the background object in the inspector i set isLowEnough to true initially then its checked whether true or not in the update method bye checking whether the position of the “Background” object is less than maxHeight where do I stipulate that the calculation of the transform.position.y is of the Background object ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody playerRb;
private Animator playerAnim;
private AudioSource playerAudio;
public ParticleSystem explosionParticle;
public ParticleSystem dirtParticle;
public AudioClip jumpSound;
public AudioClip crashSound;
public float jumpForce = 10;
public float gravityModifier;
public float maxHeight = 9.5f;
public bool isLowEnough = true;
public bool isOnGround = true;
public bool gameOver = false;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
playerAnim = GetComponent<Animator>();
playerAudio = GetComponent<AudioSource>();
Physics.gravity *= gravityModifier;
}
// Update is called once per frame
void Update()
{
//While space is pressed and player is low enough, float up
isLowEnough = (transform.position.y < maxHeight);
if (Input.GetKeyDown(KeyCode.Space) && !gameOver)
{
if(isLowEnough)
playerRb.AddForce(Vector3.up * 1 * jumpForce, ForceMode.Impulse);
isOnGround = false;
playerAnim.SetTrigger("Jump_trig");
dirtParticle.Stop();
playerAudio.PlayOneShot(jumpSound, 15.0f);
{
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
dirtParticle.Play();
} else if (collision.gameObject.CompareTag("Obstacle"))
{
Debug.Log("Game Over Woody");
gameOver = true;
playerAnim.SetBool("Death_b", true);
playerAnim.SetInteger("DeathType_int", 1);
explosionParticle.Play();
dirtParticle.Stop();
playerAudio.PlayOneShot(crashSound, 15.0f);
}
}
}
Those curly braces are important — not just to have them, but where you put them matters too. Right now your if condition on line 37 is only affecting line 38 (and the curly braces on lines 43 and 45 are doing nothing at all).
But, though the animation and sounds would still play, I wouldn’t expect the object to actually jump up when you press the space bar if its position is >= 9.5 (because the AddForce call is the one line your if(isLowEnough) check actually affects). So. Are you saying that you can get your balloon up to y=10 or y=11 or so (verifying this in the inspector), and press the spacebar, and it still jumps up even higher?
What I was trying to do was set the maxHeight at the transform.position.y from the Background object which the inspector says is “9.5” so thats what I call myself setting maxHeight to and then the isLowEnough value should always be < or = to the maxHeight value if it is equal to the maxHeight than the ballon should not go any higher so this is what I think.
I’ve tried that solution too. But it doesnt work. I think the reason for that; just under the maxHeight (for example 9.4f) you press spacebar and give the balloon jumpForce so the balloon go higher. I’ve solved this problem in a different way. I’ve duplicated “Ground” game object in the hierarchy and named it “Ceiling”. Positioned it where you want that balloon can’t go up above.(In this case maxHeight will be okay) Add Box Collider component to the ceiling. In the PlayerControllerX script add this codes
else if (other.gameObject.CompareTag(“Ceiling”))
{
playerRb.AddForce(Vector3.down * 25, ForceMode.Impulse);
playerAudio.PlayOneShot(jumpSound);
}
very end of the codes in the “OnCollusionEnter” method. (ın the code you give above after 63. line ) One more thing, if you do that you’ll notice when the balloon goes up the shade cover it because of the ceiling. You have to reposition the “Directional Light” (position(0,10,-10), rotation(30,-30,0) ).This works perfectly in my game.