Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question What makes the term '||' invalid in my ground checking script?

Discussion in 'Scripting' started by JustaDuck97, May 22, 2020.

  1. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    I'm trying to make a ground check and I have tried using Linecast and Boxcasts and I couldn't get those to work properly. They couldn't detect anything, but the term '||' as 'or' worked fine. Since my game won't have any surfaces you can't jump off of I decided to ditch the methods that need to detect layers and just go with a small Raycasts that collides with any box collider. I got it working with this:


    Code (CSharp):
    1. if(Physics2D.Raycast(groundCheck1.position, Vector2.down, 0.3f))
    2.         {
    3.             IsGrounded = true;
    4.            
    5.            
    6.         }
    7.         else
    8.         {
    9.             IsGrounded = false;
    10.          
    11.         }
    This works and the player can only jump when touching the ground, but there is an issue. I need two other raycasts to detect when the player is standing on the edge and allow it to jump because it is still literally grounded just not technically to may game through code. So I tried using the term '||' which has worked in the past, but now it is suddenly invalid in this code:

    Code (CSharp):
    1. if(Physics2D.Raycast(groundCheck1.position, Vector2.down, 0.3f)) || (Physics2D.Raycast(groundCheck2.position, Vector2.down, 0.3f)) || (Physics2D.Raycast(groundCheck3.position, Vector2.down, 0.3f))
    2.         {
    3.             IsGrounded = true;
    4.            
    5.            
    6.         }
    7.         else
    8.         {
    9.             IsGrounded = false;
    10.          
    11.         }
    it's usually something obvious with me, but I've gone over this multiple times and can't find anything helpful on online. What am I missing?
     
  2. edwiz7

    edwiz7

    Joined:
    May 29, 2016
    Posts:
    4
    You are closing the if statement after the first raycast check, remove one ')' and put it at the end of line 1.
     
  3. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    God, i'm stupid
     
  4. edwiz7

    edwiz7

    Joined:
    May 29, 2016
    Posts:
    4
    Happens to all of us haha