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 /
avatar image
0
Question by DanaScully · Oct 26, 2016 at 07:48 AM · jumpjumping object

Why the ball is not jumping in direction

Following a tutorial I was able to make a simple ball jump. However the ball jumps only on y direction and only when on the ground. My question is why the ball is not jumping when it's moving or why doesn't jump in the direction of movement (left or right). Here is how I coded:

 using UnityEngine;
 using System.Collections;

 public class BallControl : MonoBehaviour {

 // Use this for initialization
 public float rotationSpeed = 100;
 public float jumpHeight = 8;

 private bool isFalling = false;
 // Update is called once per frame
 void Update () 
 {
             //-----------Handle ball rotation 
     float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     GetComponent<Rigidbody>().AddRelativeTorque(Vector3.back * rotation);

     
     if(Input.GetKeyDown(KeyCode.Space) && isFalling == false)
     {
         GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, 0);
         
     }
     isFalling = true;
 }

 void OnCollisionStay()
 {
     isFalling = false;
 }
 }


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
1
Best Answer

Answer by tanoshimi · Oct 26, 2016 at 07:50 AM

It only jumps when you are on the ground because of isFalling == false check on line 20, which is only true if you are currently in a collision with something (line 30).

It only jumps straight up because on line 22, you are setting the y velocity to jumpheight, but both the x and z components of velocity to 0: GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, 0);

Comment
Add comment · Show 4 · 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 DanaScully · Oct 26, 2016 at 08:51 AM 0
Share

But i don't want the x and z to have velocities. I want the ball to jump in horizontal direction if I am pressing the arrow keys. Else just up direction.

avatar image ragnaros100 DanaScully · Oct 26, 2016 at 10:42 AM 0
Share

As @tanoshimi said, you are overwriting your horizontal direction completely by setting the x & z values to 0. That means your ball will allways stop it's x & z trajectory when you press the jump button. You should use AddForce or something like

GetComponent<Rigidbody>().velocity = new Vector3(velocity.x, jumpHeight, velocity.z)

(with the 'velocity' being the correct rigidbody velocity) ins$$anonymous$$d.

avatar image DanaScully ragnaros100 · Oct 26, 2016 at 11:53 AM 0
Share

Thanks for the guide. I rewrote my code like this:

 GetComponent<Rigidbody>().velocity = new Vector3(GetComponent<Rigidbody>().velocity.x,jumpHeight,GetComponent<Rigidbody>().velocity.z);

And now it works fine.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

When I jump i teleport up and then glide down 0 Answers

Another Slow Fall Problem... 2 Answers

Im doing a 3d endless runner, but i just cant get my cube(player) to jump when i press the spacebar, ive searced everywhere and tried a lot of different jump scripts but to no avail. 0 Answers

Jump character with click on space key in Javascript 1 Answer

Jumping only once (2D) 2 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