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 iblue001 · Apr 15, 2017 at 08:09 AM · movementsphere

Sphere fly's when I want it to jump

Hey guys so I am trying to make a sphere jump. Also before you say anything I have only been using unity for 3 days........ So I have this code but I can't jump. I have searched for about 2 hours but couldn't find anything to work. I have developed some apps in java but want to get into gaming. I have not learnt c# and I am basing everything off what I have learnt in java. Btw I could have used switch statements but do not want to use them. I had some code before which I was able to jump but it would only jump backwards

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
     public Rigidbody rbS;
     public float movementSpeed;
     private bool jump;
 
 
     // Time.deltaTime makes everything frame rate independent
    
 
     private void Start()
     {
         rbS = GetComponent<Rigidbody>(); 
     }
     void FixedUpdate () {
         if (Input.GetKey("a")) // This allows you to move left
         {
             rbS.AddForce(-movementSpeed * Time.deltaTime, 0, 0); // Adding a force on the x axis
         }
         else if (Input.GetKey("d")) // This allows you to move right
         {
             rbS.AddForce(movementSpeed * Time.deltaTime, 0, 0); // Adding a force on the x axis
         }
         else if (Input.GetKey(KeyCode.Space)) // This is supposed to make you jump
         {
             rbS.AddForce(0, movementSpeed * Time.deltaTime, 0);  // Adding a force on the y axis
         }
         else if (Input.GetKey("w")) // This is supposed to make you go forward
         {
             rbS.AddForce(0, 0, movementSpeed * Time.deltaTime); // Adding a force on the z axis
             
         }
        
         else if (Input.GetKey("s")) // This is supposed to make you go back
         {
             rbS.AddForce(0, 0, -movementSpeed * Time.deltaTime); // Adding a force on the z axis
         }
 
        
         
     }
     
 }
 
Comment
Add comment · Show 3
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 Xarbrough · Apr 16, 2017 at 12:09 AM 0
Share

Have you tried debugging your code? When you add breakpoints or log statements, does the rbS.AddForce line for jumping get called? Is movementSpeed big enough? Have you tried different settings? What are the settings on the rigidbody? Is it maybe set to kinematic?

avatar image iblue001 Xarbrough · Apr 16, 2017 at 05:26 AM 0
Share

well it can jump but if i hold my spacebar it just keeps on going

avatar image Ali-hatem · Apr 16, 2017 at 01:20 PM 1
Share

@sleepandpancakes is right and put the input code in Update() so you don't miss an input

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by sleepandpancakes · Apr 16, 2017 at 05:46 AM

Change Input.GetKey(KeyCode.Space) to Input.GetKeyDown(KeyCode.Space) so that it only adds the force once when you press the button, rather than adding force the entire time the space button is pressed. You probably also want to specify the ForceMode as Impulse, i.e. rbS.AddForce(0, forceValue, 0, ForceMode.Impulse);

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 iblue001 · Apr 17, 2017 at 10:32 AM 0
Share

Yea its still not jumping. I have my mass set to 35, gravity is on, drag is 0 and angular drag is 0.05.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     public Rigidbody rbS;
     public float movementSpeed;
     private bool jump;
 
 
     // Time.deltaTime makes everything frame rate independent
    
 
     private void Start()
     {
         rbS = GetComponent<Rigidbody>(); 
     }
 
    
     void FixedUpdate () {
         if (Input.Get$$anonymous$$ey("a")) // This allows you to move left
         {
             rbS.AddForce(-movementSpeed * Time.deltaTime, 0, 0, Force$$anonymous$$ode.Impulse); // Adding a force on the x axis
         }
         else if (Input.Get$$anonymous$$ey("d")) // This allows you to move right
         {
             rbS.AddForce(movementSpeed * Time.deltaTime, 0, 0, Force$$anonymous$$ode.Impulse); // Adding a force on the x axis
         }
         else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) // This is supposed to make you jump
         {
             rbS.AddForce(0, movementSpeed * Time.deltaTime, 0, Force$$anonymous$$ode.Impulse);  // Adding a force on the y axis
         }
         else if (Input.Get$$anonymous$$ey("w")) // This is supposed to make you go forward
         {
             rbS.AddForce(0, 0, movementSpeed * Time.deltaTime, Force$$anonymous$$ode.Impulse); // Adding a force on the z axis
             
         }
        
         else if (Input.Get$$anonymous$$ey("s")) // This is supposed to make you go back
         {
             rbS.AddForce(0, 0, -movementSpeed * Time.deltaTime, Force$$anonymous$$ode.Impulse); // Adding a force on the z axis
         }
 
        
         
     }
     
 }
 

avatar image sleepandpancakes iblue001 · Apr 17, 2017 at 05:38 PM 0
Share

Since Time.deltaTime is a small value, you are adding very little force to the sphere. Change movementSpeed * Time.deltaTime to a different float forceValue and increase it. Or you might want to use a VelocityChange Force$$anonymous$$ode ins$$anonymous$$d of an Impulse. Read more about them here

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

7 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Movement Bug 1 Answer

Joystick controller problem walking on sphere 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Using WASD keys instead of mouse to roll ball? 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