Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by mblissmer · Sep 05, 2015 at 06:37 PM · physicsaddforce

My sphere won't always jump when space is pressed

Hey all!

Let's get right to it:

I made the roll-a-ball thing, like a lot of people did, and then I started bolting things on, which I assume a lot of people did as well. Here's where I'm currently stuck:

Right now I have a raycast pointing straight down at 0.9 length, to check to see if the ball is grounded. If it is, it sets "grounded" to "true"

If "grounded" is true, and you hit Space, you should jump.

The "grounded" bool works great. I have it public, so I can watch it check every time.

The weird thing is, this only happens some of the time. The grounded checkbox will be checked, and jump does nothing. If you keep rolling around and try some more, eventually you'll start jumping. Seems to come and go. At this point, I'm assuming I am using a wrong way to make the sphere jump. I want it to keep momentum, but just pop into the air in whatever direction the player is rolling.

Here's what I've got:

     //player movement variables
     private float speed = 10f;
     private float jumpForce = 20f;
     public bool grounded;
     private RaycastHit hit;
     private float dist;
     private Vector3 direction;
     private float airControlModifier = 0.1f;    
 
     void Start ()
     {
         Debug.Log ("Level name is: " + Application.loadedLevelName);
         rb = GetComponent<Rigidbody> ();
         grounded = false;
     }
 
 
     void Update (){
         dist = 0.9f;
         direction = Vector3.down;
         Debug.DrawRay(transform.position,direction*dist,Color.green);
         if (Physics.Raycast (transform.position, direction, dist)) {
             grounded = true;
         } else {
             grounded = false;
         }
     }
 
     // update every physics frame
     void FixedUpdate ()
     {
         // get Axis input
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         //see if player is grounded, if not, modify the movement speed, and either way, add force to player
         if (grounded == true) {
             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
             rb.AddForce (movement*speed);
         }
         else {
             Vector3 movement = new Vector3 (moveHorizontal * airControlModifier, 0.0f, moveVertical * airControlModifier);
             rb.AddForce (movement*speed);
         }
 
         // Jump with space bar
         if (Input.GetKeyDown (KeyCode.Space) && grounded == true) {
             //rb.velocity = new Vector3 (moveHorizontal, jumpForce, moveVertical);
             Vector3 movement = new Vector3 (moveHorizontal, jumpForce, moveVertical);
             rb.AddForce (movement*speed);
         }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by mblissmer · Sep 05, 2015 at 06:46 PM

And of course the second I ask the question, a lightbulb goes off and I solve it.

I moved the jump command up to Update instead of Fixed Update. Since GetKeyDown only triggers for that single frame, Fixed Update was missing them randomly.

I moved it to Update and BAM. Works great!

Comment
Add comment · 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

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to control the speed of AddForce (ForceMode.Impulse)????? 1 Answer

Addforce in fixedupate 0 Answers

AddForce only working inside another object's collider 0 Answers

How do I get objects in front of the player to be blasted away? 1 Answer

Ball Speed is not increasing as per code 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