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
0
Question by c====3 · Nov 08, 2013 at 10:51 AM · collisionjumpboolgroundedroll

How to keep ball from jumping more than once?

I am trying to make a ball jump as it is rolling but when it is rolling, the isGrounded bool keeps flashing on and off. Why and how do I fix it? I would appreciate any help at all.

C# preffered.

public class Jump : MonoBehaviour {

 public float jumpForce = 10.0f;
 public bool isGrounded;
 public Collider groundCollider;
 float jumpWaitTime = 1.0f;
 private float nextJumpTime;
 
 
 // Use this for initialization
 void Start ()
 {
     
 isGrounded = true;
 }
 //performs a jump 
 public void JumpFunction() 
 {
 
    Vector3 jump = new Vector3(0.0f, jumpForce, 0.0f);
    rigidbody.AddForce(jump, ForceMode.Impulse);
 }
 
 // Update is called once per frame
 void Update () 
 {
      if (Physics.Raycast(transform.position, -transform.up, 2)) 
    {
      isGrounded = true;
    } 
 else 
    {
      isGrounded = false;
    }
 //jump if player presses Jump
    if(Input.GetButtonDown("Jump") && isGrounded && Time.time >= nextJumpTime)
     {
         isGrounded = false;
       JumpFunction();
          nextJumpTime = Time.time + jumpWaitTime;
    }
     // manually set isGrounded to true
     if(Input.GetKeyDown(KeyCode.U))
     {
         isGrounded = true;
     }
     
 
 }
 
 void FixedUpdate ()
 {
     
 }
 void onCollisionStay(Collider groundCollider)
 {
     
     isGrounded = true;
     
 }

}

Comment
Add comment · Show 2
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 mattssonon · Nov 08, 2013 at 10:56 AM 0
Share

Well, is it because isGrounded gets changed in the if (Physics.Raycast(transform.position, -transform.up, 2)) condition? If so, are you sure 2 is enough? And is -transform.up always pointing at whatever the ball is rolling on?

avatar image KiraSensei · Nov 08, 2013 at 10:58 AM 0
Share

You can use some Debug.Log lines in your code, every time isGrounded can change. That way, you will know what is going on. In my opinion it is the raycast that can't stop changing the value, but I don't know why yet...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by anirudhp9708 · Jan 21, 2017 at 06:45 AM

@ c====3 Yo bro, I got it. Although I used movement only for horizontal axis.. :)

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class BallControl : MonoBehaviour {

 public float speed;
 public int jumpHeight = 8;
 private bool isfalling = false;
 private Rigidbody rb;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
     isfalling = false;
 }



 void FixedUpdate()
 {

     float moveHorizontal = Input.GetAxis("Horizontal");

     Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f);

     rb.AddForce(movement * speed);

     if (Input.GetKeyDown(KeyCode.W) && isfalling == false)
     {
         rb.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
        
     }
     isfalling = true;

 }


    void OnCollisionStay(Collision groundcollider)
 {
     isfalling = false;
 }

}

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

18 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

Related Questions

Grounded bool acting weird 1 Answer

jump on collision - first person controller 0 Answers

Jumping on top of an enemy problem 1 Answer

Character jump on collision 1 Answer

Check if skateboard is grounded 1 Answer


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