Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by connor34211 · Apr 30, 2015 at 07:29 PM · c#unity 5scriptingbasics

Beginner Question

I just recently started using Unity and am making a simple script that has a cube jump when you hit space. I tried to make it so you could only jump once until you were grounded again but for some reason you can jump twice (No more than twice) instead of once. Since the object starts at y = 0.5 I tried to make it so while it was above that you couldn't jump. I know there are much easier ways to do this and I would like to know them but I was trying to figure it out myself.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class JumpingCube : MonoBehaviour {
 
     public int jumpForce = 10;
     private bool grounded = true; 
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if (grounded)
             if (Input.GetButtonDown ("Jump")) {
                 grounded = false;
                 GetComponent<Rigidbody> ().AddForce (0, jumpForce, 0); 
                  }
 
         if (transform.position.y <= .5)
             grounded = true;
 
                  
 
 
     
                     }
 }

 



 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheShadyColombian · Apr 30, 2015 at 08:50 PM 0
Share

A little suggestion: Change your script from

          if (grounded){
              if (Input.GetButtonDown ("Jump")) {
                  grounded = false;
                  GetComponent<Rigidbody> ().AddForce (0, jumpForce, 0); 
              }
          }

to

          if (grounded && Input.GetButtonDown ("Jump")){
               grounded = false;
               GetComponent<Rigidbody> ().AddForce (0, jumpForce, 0); 
          }

(put both of the "if" statements' conditions in the same "if" statement)

it's a bit neater and easier to read. At least for me.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TheShadyColombian · Apr 30, 2015 at 08:53 PM

I suggest using a Trigger collider under your GameObject and checking if it has been triggered to enable the "grounded" bool, as if it lands and it is higher than 0.5 in the y axis it wouldn't jump.

Obviously, ignore this if that is irrelevant for your game :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VRKeith · May 01, 2015 at 06:04 PM 0
Share

I agree with this - or, at least, a Raycast would also do the trick. This would help if your character controller was doing more platfor$$anonymous$$g than just the single plane at .5

avatar image TheShadyColombian · May 01, 2015 at 07:31 PM 0
Share

True! Though (I find) this involves a lot more program$$anonymous$$g, so she/he might prefer the collider, but raycas is a lot better, specially for fast collisions (bullets etc.)

avatar image
0

Answer by VRKeith · May 01, 2015 at 03:32 PM

Unity has a function for performing Rigidbody physics in an update loop. Try changing the function from

void Update()

to

void FixedUpdate()

Also, make that an else if for the second condition.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eno-Khaon · May 01, 2015 at 08:02 PM 0
Share

While FixedUpdate() is the proper place for physics processing, it's not an adequate location for input recognition. For the best interaction between input and physics, you would want to carefully utilize a mixture of input in Update() and use of flags set by that input in FixedUpdate().

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues with jump control for player 1 Answer

Add arrays to a big array? 2 Answers

Is there a way I could combine these scripts? If so, how? 1 Answer

How to display dynamic text which will be recieved from a server like RestAPI on to the AR camera in Vuforia? 0 Answers

Looking for good resources on modifying unity SA 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges