Search Unity

Question can someone help me with my code of mine

Discussion in 'Scripting' started by Sleepy054, May 24, 2023.

  1. Sleepy054

    Sleepy054

    Joined:
    May 11, 2023
    Posts:
    6
    I made a jump button and when I spam press the space bar it makes me fly
    here's the code:

    Code (CSharp):
    1.   if (Input.GetButtonDown("Jump"))
    2.         {
    3.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    4.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    It looks like perhaps you have not finished with whatever tutorial or example code you are working with.

    For instance, the above code lacks any way to check if you are on the ground.

    If the tutorial you are working with doesn't have this, go find one that does because there are probably a half dozen or more separate things that must be set up to make it work.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    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. If you want to learn, you MUST do Step 2.

    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!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Yep. You press the button, the player goes up. That's exactly the code you wrote. So what's the problem?
     
    MatanYamin likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Yes, it appears you made a fly button, not a jump button.

    Again, lotsa steps highly dependent on what you are doing (physics, CC, something else?) so start with tutorials.

    See above for how.
     
  5. Sleepy054

    Sleepy054

    Joined:
    May 11, 2023
    Posts:
    6
    how do i stop it from flying away
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Same way it works in real life. Here you cant jump again while in the air. In your implementation you can.

    As Kurt implied, there is many ways to implement a ground check, but you simply did not provide enough information for us to help you with it. Since jumping is a kinda common thing in games you could also try to follow one of the zillion tutorials on it.
     
  7. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    110
    Yeah, we're gonna need a bit more to be able to help you.

    Are you using RigidBody on the player? Do you use gravity and freeze X/Y/Z positions?
     
  8. Sleepy054

    Sleepy054

    Joined:
    May 11, 2023
    Posts:
    6
    I am not using a rigidbody I am using a script for gravity
     
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    Well, that does not necessarily matter. Though you should think about when you consider your player to be "grounded". That's the only moment your player should be able to jump. How do you stop the player from falling without a rigidbody? So how does your gravity work?
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    If you are using a Character Controller, those come with an isGrounded flag.
    https://docs.unity3d.com/ScriptReference/CharacterController-isGrounded.html

    You know.. if you want help, putting some effort into it does not hurt. All you posted so far is low effort half-liners and we constantly have to repeat questions and pull every bit of information out of you. Thats not fun. Just post your code, like all of it, using code tags, if you cant properly describe the relevant parts of it. People have been hinting at needing more information all this time. We still dont know if you may be using a CharacterController or not. If you do, check the link above. If not, cast a ray downwards and check your distance to the ground or something. Or, you know, as suggested multiple times, check out some tutorials and do what they do. It's a really common topic.
     
    Last edited: May 25, 2023
    Kurt-Dekker and Bunny83 like this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    It appears at this stage, as Yoreki notes, you may be expecting us to play 21 questions with you.

    CharacterControllers and jumping are dime a dozen. I probably write on average one or two a month and each one is a little snowflake, different from every other one I wrote, with or without jump, with or without crouch, fly, slide, whatever it is.

    Go do a video tutorial on the motion you want, just get 'er done. It's not hard. Really. Just ... GO.

    Once again,

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    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. If you want to learn, you MUST do Step 2.

    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!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Just to put it simply, a lot of games have the behavior where the player can jump only if they are on the ground, and if they are in the air they cannot jump. I assume that's what you want.

    So in addition to checking if the player is pressing the jump button you also want to check if they are on the ground or not. There are a lot of different ways to do this depending on what type of game you are making and what type of character controller you are building.

    I tend to use raycasts because you can further use them to get info about what the player is standing on, but that may or may not matter to you. If you google how to do a ground check you'll probably find lots of different ways.
     
    Ryiah, Bunny83 and Kurt-Dekker like this.