Search Unity

How best to reference a Game Object to perform a if mathematical condition is met?

Discussion in 'Getting Started' started by Ufreewoody, Jan 15, 2020.

  1. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerControllerX : MonoBehaviour
    5. {
    6.     public bool gameOver;
    7.     public float floatForce;
    8.     private float gravityModifier = 2.5f;
    9.     private Rigidbody playerRb;
    10.     public ParticleSystem explosionParticle;
    11.     public ParticleSystem fireworksParticle;
    12.     public bool isLowEnough;
    13.     private float ballonHeight;
    14.     private AudioSource playerAudio;
    15.     public AudioClip moneySound;
    16.     public AudioClip explodeSound;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         ballonHeight = gameObject.GetComponent<>
    21.         playerRb = GetComponent<Rigidbody>();
    22.         Physics.gravity *= gravityModifier;
    23.         playerAudio = GetComponent<AudioSource>();
    24.         // Apply a small upward force at the start of the game
    25.         playerRb.AddForce(Vector3.up * 5, ForceMode.Impulse);
    26.     }
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         // While space is pressed and player is low enough, float up
    31.         if (Input.GetKey(KeyCode.Space) && !gameOver && isLowEnough <= Transform.Background.position.y;
    32.         {
    33.             isLowEnough = true;
    34.             playerRb.AddForce(Vector3.up * 1 * floatForce, ForceMode.Impulse);
    35.         }
    36.     }
    37.     private void OnCollisionEnter(Collision other)
    38.     {
    39.         // if player collides with bomb, explode and set gameOver to true
    40.         if (other.gameObject.CompareTag("Bomb"))
    41.         {
    42.             explosionParticle.Play();
    43.             playerAudio.PlayOneShot(explodeSound, 1.0f);
    44.             gameOver = true;
    45.             Debug.Log("Game Over!");
    46.             Destroy(other.gameObject);
    47.         }
    48.         // if player collides with money, fireworks
    49.         else if (other.gameObject.CompareTag("Money"))
    50.         {
    51.             fireworksParticle.Play();
    52.             playerAudio.PlayOneShot(moneySound, 1.0f);
    53.             Destroy(other.gameObject);
    54.         }
    55.     }
    56. }
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    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;

    Code (CSharp):
    1. void Update()
    2.     {
    3.         // While space is pressed and player is low enough, float up
    4.         isLowEnough = (transform.Position.y < maxHeight); // Note maxHeight must be defined above
    5.         if (Input.GetKey(KeyCode.Space) && !gameOver) ;
    6.         {
    7.             if (isLowEnough)
    8.             {
    9.             playerRb.AddForce(Vector3.up * 1 * floatForce, ForceMode.Impulse);
    10.             }
    11.         }
    12.     }
     
    Last edited: Jan 15, 2020
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    But if you do try that, you'll need to fix the syntax on line 5... that final semicolon should be a close parenthesis, or so it appears to me.
     
    Bill_Martini likes this.
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Oops! @JoeStrout is correct. Always happens when I freestyle. I fixed the example.
     
  5. Fleurpink

    Fleurpink

    Joined:
    Jan 16, 2020
    Posts:
    2
    Did you fixe the syntax in the 5 line? And thank you for your help
     
    Last edited: Jan 18, 2020
  6. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
     
  7. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    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 ?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private Rigidbody playerRb;
    8.     private Animator playerAnim;
    9.     private AudioSource playerAudio;
    10.     public ParticleSystem explosionParticle;
    11.     public ParticleSystem dirtParticle;
    12.     public AudioClip jumpSound;
    13.     public AudioClip crashSound;
    14.     public float jumpForce = 10;
    15.     public float gravityModifier;
    16.     public float maxHeight = 9.5f;
    17.     public bool isLowEnough = true;
    18.     public bool isOnGround = true;
    19.     public bool gameOver = false;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         playerRb = GetComponent<Rigidbody>();
    25.         playerAnim = GetComponent<Animator>();
    26.         playerAudio = GetComponent<AudioSource>();
    27.         Physics.gravity *= gravityModifier;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         //While space is pressed and player is low enough, float up
    34.         isLowEnough = (transform.position.y < maxHeight);
    35.         if (Input.GetKeyDown(KeyCode.Space) && !gameOver)
    36.         {
    37.             if(isLowEnough)
    38.             playerRb.AddForce(Vector3.up * 1 * jumpForce, ForceMode.Impulse);
    39.             isOnGround = false;
    40.             playerAnim.SetTrigger("Jump_trig");
    41.             dirtParticle.Stop();
    42.             playerAudio.PlayOneShot(jumpSound, 15.0f);
    43.             {
    44.  
    45.             }
    46.         }
    47.     }
    48.     private void OnCollisionEnter(Collision collision)
    49.     {
    50.             if (collision.gameObject.CompareTag("Ground"))
    51.         {
    52.             isOnGround = true;
    53.             dirtParticle.Play();
    54.         }   else if (collision.gameObject.CompareTag("Obstacle"))
    55.         {
    56.             Debug.Log("Game Over Woody");
    57.             gameOver = true;
    58.             playerAnim.SetBool("Death_b", true);
    59.             playerAnim.SetInteger("DeathType_int", 1);
    60.             explosionParticle.Play();
    61.             dirtParticle.Stop();
    62.             playerAudio.PlayOneShot(crashSound, 15.0f);
    63.         }
    64.     }
    65. }
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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?
     
  9. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    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.

    Thanks so much for your help.
     
  10. djomarfar

    djomarfar

    Joined:
    Jun 4, 2020
    Posts:
    1
    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.