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 Samael_00001 · Jul 25, 2014 at 05:54 PM · jumpforcesphereball

How to make Sphere to jump?

I don't know, how to find out if a Phere is already in the air (we can jump only when we are on a ground, right?)

Thanks!!

I'm using this script to move along:

 using UnityEngine;
 using System.Collections;
 
 public class BallMovement : MonoBehaviour {
 
     public float speed;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
        
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rigidbody.AddForce(movement * speed * Time.deltaTime);
        
     }
 }
 
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 AndyMartin458 · Jul 25, 2014 at 06:04 PM 0
Share

Don't be confused by the code comment, FixedUpdate is not called once per frame.

2 Replies

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

Answer by AndyMartin458 · Jul 25, 2014 at 06:10 PM

I assume that your sphere has a collider? You can do a check for when it is on the ground with OnCollision commands. Try this. http://docs.unity3d.com/ScriptReference/Collider.OnCollisionExit.html

 bool isGrounded = false;
 
 void OnCollisionEnter(Collider other)
 {
     if(other.gameObject.tag == "ground")
         isGrounded = true;
 }

 void OnCollisionExit(Collider other)
 {
     if(other.gameObject.tag == "ground")
         isGrounded = false;
 }

Also, nothing in your code seems to keep you from jumping while you are already in the air. Maybe something like this?

 Vector3 movement = Vector3.zero;
 void FixedUpdate () 
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
  
     //Don't allocate new memory in FixedUpdate!!!!!
     movement.x = moveHorizontal;
     movement.z = moveVertical;

     if(isGrounded == true)
         rigidbody.AddForce(movement * speed * Time.deltaTime);
 }
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 Samael_00001 · Jul 26, 2014 at 11:33 AM 0
Share

thanks a lot! )

avatar image
0

Answer by Samael_00001 · Jul 26, 2014 at 11:34 AM

Here is my Solution:

 using UnityEngine;
 using System.Collections;
 
 public class BallMovement : MonoBehaviour {
 
     public float speed;
     public float jumpSpeed;
     bool CanJump;
     bool jump;
 
     
     void Start () 
     {
         CanJump = true;
         jump = false;
     }
 
     void OnCollisionExit(Collision collisionInfo)
     {
         CanJump = false;
         jump = false;
     }
 
     void OnCollisionEnter(Collision collisionInfo)
     {
         CanJump = true;
     }
 
     void Update()
     { 
        if (Input.GetKeyDown(KeyCode.Space) && CanJump == true)
         {
            jump = true;
         }
     }
 
     
     void FixedUpdate () {
        
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rigidbody.AddForce(movement * speed * Time.deltaTime);
 
         if (jump == true)
         {
             rigidbody.AddForce(Vector3.up * jumpSpeed);
         }
         
     }
  
 }
 
Comment
Add comment · Show 3 · 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 AndyMartin458 · Jul 26, 2014 at 08:07 PM 0
Share

@Samael_00001 I understand that you needed to make modifications, but isn't this equivalent to my answer?

avatar image AndyMartin458 · Jul 26, 2014 at 08:11 PM 0
Share

@Samael_00001 Would you be so kind as to make this a comment on my answer and select $$anonymous$$e? It's just proper answers forum etiquette.

avatar image Samael_00001 · Jul 27, 2014 at 11:55 AM 0
Share

@Andy$$anonymous$$artin458, sure )

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

25 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

Related Questions

A node in a childnode? 1 Answer

Ball jump on collision problem 1 Answer

Increase Jump Through Mouse Clicks? 1 Answer

How do I check when a rigidbody is colliding with the floor? 2 Answers

Movement problem? 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