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. Dismiss Notice

Need Help... Im stuck

Discussion in 'Scripting' started by DiagonalDominusX, May 21, 2021.

  1. DiagonalDominusX

    DiagonalDominusX

    Joined:
    Mar 6, 2021
    Posts:
    8
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour {
    public float movementSpeed;
    public Rigidbody2D rb;

    public float jumpForce = 20f;
    public Transform feet;
    public LayerMask groundLayers;

    float mx;

    private void Update() {
    mx = Input.GetAxisRaw("Horizontal");

    if (Input.GetButtonDown("Jump") && IsGrounde()) {
    Jump();
    }

    }

    private void FixedUpdate() {
    Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);

    rb.velocity = movement;
    }

    void Jump () {
    Vector2 movement = new Vector2(rb.velocity.x, jumpForce);

    rb.velocity = movement;
    }

    public bool IsGrounded() {
    Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);

    if (groundCheck) !- null;) {
    return true;

    }

    return false;
    }
    }
    upload_2021-5-21_18-4-19.png

    I don't know what I'm supposed to do as I have already tried to do what it says with my code but it doesn't work. Please help me.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Please use CODE tags as it is described HERE.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour {
    4.     public float movementSpeed;
    5.     public Rigidbody2D rb;
    6.  
    7.     public float jumpForce = 20f;
    8.     public Transform feet;
    9.     public LayerMask groundLayers;
    10.  
    11.     float mx;
    12.  
    13.     private void Update() {
    14.         mx = Input.GetAxisRaw("Horizontal");
    15.  
    16.         if (Input.GetButtonDown("Jump") && IsGrounde()) {
    17.             Jump();
    18.         }
    19.  
    20.     }
    21.  
    22.     private void FixedUpdate() {
    23.         Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
    24.  
    25.         rb.velocity = movement;
    26.     }
    27.  
    28.     void Jump () {
    29.         Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
    30.  
    31.         rb.velocity = movement;
    32.     }
    33.  
    34.     public bool IsGrounded() {
    35.         Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
    36.  
    37.         if (groundCheck) !- null;) {
    38.             return true;
    39.  
    40.         }
    41.  
    42.         return false;
    43.     }
    44. }
    There are two problems. minus sign instead of equals and what the ; is doing there?
     
  3. DiagonalDominusX

    DiagonalDominusX

    Joined:
    Mar 6, 2021
    Posts:
    8
    Did not work. Same error.
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Post the updated code (with code tags).
     
    Lurking-Ninja likes this.
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    What "did not work" exactly? Please post your actual code. The whole thing in code tags so we can see it.

    ps: yeah, forgot to mention here (I mentioned in your other post): you're closing the if apparently early. If you want to include the rest in the if statement you shouldn't close the parenthesis after the groundCheck).
     
    Last edited: May 21, 2021
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    The code you have attempted to show in your original post is hard to read without code tags, which makes finding the problem all the more harder. After @Lurking-Ninja kindly did you the service of putting it in code tags in his own reply, it is clear your code has 3 problems, which is impossible to attribute to a copy mistake, or your actual code. If people reading your post can't understand the situation you are in, we can't help you.

    We can certainly guess the problem, but it will be a far longer process than it needs to be.

    Referencing the copied code in code tags by @Lurking-Ninja:
    Line 16, there should be a complaint that "IsGrounde" does not exist, but this is probably a copy typo
    Line 37, this should be where the 2 remaining issues lie.

    Not being able to solve that problem by yourself is clear indication you do not understand your fundamentals, and forums are not for teaching fundamentals. Giving you the solution will most likely have you coming back to the forums again with another trivial question.

    For those reasons, all I will offer, is to read up on how an "if" statement works:
    https://www.w3schools.com/cs/cs_conditions.asp
    After you have armed yourself with that knowledge, read the pointers from @Lurking-Ninja again, and solve the problem yourself.
     
  7. DiagonalDominusX

    DiagonalDominusX

    Joined:
    Mar 6, 2021
    Posts:
    8
    Heres The Code In Code Tags Or These Things Or Something Like This
    Sorry for me being a newbie. I am new to scripting and Unity.
    Code (csharp):
    1.  
    2. [code=CSharp]using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     public float movementSpeed;
    7.     public Rigidbody2D rb;
    8.  
    9.     public float jumpForce = 20f;
    10.     public Transform feet;
    11.     public LayerMask groundLayers;
    12.  
    13.     float mx;
    14.  
    15.     private void Update() {
    16.         mx = Input.GetAxisRaw("Horizontal");
    17.      
    18.         if (Input.GetButtonDown("Jump") && IsGround()) {
    19.             Jump();
    20.     }
    21.  
    22. }
    23.  
    24.     private void FixedUpdate() {
    25.     Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
    26.  
    27.     rb.velocity = movement;
    28.     }
    29.  
    30.     void Jump () {
    31.         Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
    32.  
    33.         rb.velocity = movement;
    34.     }
    35.  
    36.     public bool IsGrounded() {
    37.         Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
    38.  
    39.         if (groundCheck) !- null;) {
    40.             return true;
    41.  
    42.         }
    43.  
    44.         return false;
    45.     }
    46. }
     
  8. DiagonalDominusX

    DiagonalDominusX

    Joined:
    Mar 6, 2021
    Posts:
    8
    The image that I sent on my original post keeps coming up. Even with edited code and help from other pple.
     
  9. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    70
    There were quite a few problems, errant brackets and parentheses; this should be cleaned up (tested fine in editor). Note that I'm simply performing a structural edit here, not a functional one.

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float movementSpeed;
    8.     public Rigidbody2D rb;
    9.     public float jumpForce = 20.0f;
    10.     public Transform feet;
    11.     public LayerMask groundLayers;
    12.     float mx;
    13.  
    14.     private void Update()
    15.     {
    16.         mx = Input.GetAxisRaw("Horizontal");
    17.    
    18.         if (Input.GetButtonDown("Jump") && IsGrounded())
    19.             Jump();
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
    25.         rb.velocity = movement;
    26.     }
    27.  
    28.     void Jump ()
    29.     {
    30.         Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
    31.         rb.velocity = movement;
    32.     }
    33.  
    34.     public bool IsGrounded()
    35.     {
    36.         Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
    37.         if (groundCheck != null)
    38.         {
    39.             return true;
    40.         }
    41.         return false;
    42.     }
    43. }
    44.  
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    You haven't fixed anything I pointed out, what do you expect?
     
    Joe-Censored likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Since this is not a "correct your typos" service forum, here is how to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Note that you should be using a code editor which highlight these errors to you as soon as they appear. VS for example.