Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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
1
Question by pnut03 · Aug 02, 2015 at 06:34 PM · jumpcubenewbiesimple

Can someone help me make a simple jump script?

Hello im just making a game that is VERY simple and im really really new to this. Im coding in c# and i want to make a jump script where the cube just stands still and jumps. I dont know what to write in the code. I tried to do somethings but it didnt work as intended. I guess you could use this to help me and maybe ill learn. I also havent set up to check if its touching the ground, i was just trying to find a way to make it jump first then ill do that.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     //Spawn Variables
     public float playerSetX;
     public float playerSetY;
     public float playerSetZ;
 
 
     //Movement Variables
     public float playerJumpHeight = 2;
 
     void Start () {
 
         //Player Spawn Point
 
         //This is where our player will start when the game is played.
 
         //player == game object. Game object == transform!
         transform.position = new Vector3(playerSetX, playerSetY, playerSetZ);
     }
 
     void Update () {
 
         //player to move up jumping
 
          //player (gameobject) aka transform to move when i press the arrow keys or keyboard keys
 
         //jump
         if (Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.D) || Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow)) {
         
             transform.position += new Vector3(playerSetX, playerJumpHeight, playerSetZ);
             }
     }
 
 }


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 Akshat102 · Oct 17, 2021 at 02:00 PM 0
Share

I use all method but it is not working!

6 Replies

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

Answer by Positive7 · Aug 02, 2015 at 07:26 PM

You might want to check some tutorial at http://unity3d.com/learn

 using UnityEngine;
 using System.Collections;
     
     [RequireComponent(typeof(Rigidbody))]
     public class PlayerController : MonoBehaviour {
     
         public Vector3 jump;
         public float jumpForce = 2.0f;
     
         public bool isGrounded;
         Rigidbody rb;
         void Start(){
             rb = GetComponent<Rigidbody>();
             jump = new Vector3(0.0f, 2.0f, 0.0f);
         }
     
         void OnCollisionStay()
         {
             isGrounded = true;
         }
     
         void Update(){
             if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
     
                 rb.AddForce(jump * jumpForce, ForceMode.Impulse);
                 isGrounded = false;
             }
         }
     }


Comment
Add comment · Show 9 · 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 pnut03 · Aug 05, 2015 at 05:59 PM 0
Share

Do you $$anonymous$$d explaining it please? I did ask just for a jump script but i thought i could just try to read and learn it. I also did alot of unity's learn pages but still dont understand how to make a jump script. Sorry if im annoying you... And btw that jump script did work.

avatar image imilanspinka · Aug 05, 2015 at 06:38 PM 2
Share

@pnut03 I will try although I did not write this answer.

[RequireComponent(typeof(Rigidbody))] > requires the rigidbody component on your GameObject

public Vector3 jump; Vector3 is a variable storing three axis values. In this case it basically contains the information in which direction will the object move.

public bool isGrounded; a boolean deter$$anonymous$$ing whether the GameObj is touching (colliding) with the ground. The bool itself does nothing, we have to set it true while the player collides with the ground, by doing

 void OnCollisionStay()
 {
      isGrounded = true;
 }

Phew. Then, there's Rigidbody rb;. See what rigidbody is.

in Start(), we assign the variable rb to the component attached to your GameObj and also we assign values to the Vector3 jump.

Finally, in Update(), this code:

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && isGrounded)
 {     
     rb.AddForce(jump * jumpForce, Force$$anonymous$$ode.Impulse);
     isGrounded = false;
 }

means that if the player hits the Space button and at the same time, the GameObj is grounded, it will add a physic force to the rigidbody, using

 AddForce(Vector3 force, Force$$anonymous$$ode mode)

where force is the Vector3 storing the movement info and mode is how the force will be applied (mode can be Force$$anonymous$$ode.Force, Force$$anonymous$$ode.Acceleration, Force$$anonymous$$ode.Impulse or Force$$anonymous$$ode.VelocityChange, see Force$$anonymous$$ode for more).

If you still don't understand something, feel free to let me know!

avatar image Major0 · Aug 05, 2015 at 07:44 PM 0
Share

@pnut03 I suggest going through both feature-specific (as in Scripting for Beginners or Physics for Beginners etc...) AND full-project video tutorials (as in Roll-A-Ball project and then Survival Shooter Tutorial) found here and here. Also Live Training Archive is a good place to head to next since it covers best practices.

Also, @imilanspinka was being nice here to explain the code but mostly you have to solve the problems you're going through. You can still consult the community here after trial and error on specific issues you are having; that doesn't mean anybody would hesitate to help if they can!

Good luck and have fun :D cheers :)

avatar image lluiscolom8 · Jan 02, 2018 at 10:22 AM 0
Share

Hi man, I tried the script and it works good. However, there are several issues that should be fixed:ç

When the player is jumping, if you hit the space again, it makes another small jump. This only happen once per jump, so you can't "fly", but it's annoying. Any way to solve this?

The length of the jump when the player is running is longer than it should be. How can I fix that too? Thank you!!

avatar image tibo_declercq lluiscolom8 · Mar 04, 2018 at 08:02 PM 0
Share

Hope i'm not too late, but first of all thank you, second of all, I found a way to prevent your double jumping.

private void OnCollisionStay() { if(!isGrounded && rb.velocity.y == 0) { isGrounded = true; //jumpsLeft = 1; } }

it helps to double check your Y-velocity when you collide, otherwise you reset your "isGrounded" before your object even left the floor. Hope this helps

avatar image greekdude tibo_declercq · Mar 10, 2018 at 05:38 PM 0
Share

Unfortunately, this causes issues for me when I am walking up a slope

avatar image labradorbrownie305 · Apr 30, 2018 at 01:40 AM 0
Share

I wrote all of the same code.What about the RequirementComponent part? $$anonymous$$icrosoft Visual Studio (where I typed in the code) doesn't explain what the problem is. Same as Vector3, is there a way to fix all this?

Show more comments
avatar image
2

Answer by drearyplane · May 11, 2019 at 09:51 AM

I know I'm three years late but here's my two cents - the doyble jump is annoying but there's a simple fix:

  using UnityEngine;
  using System.Collections;
      
      public class PlayerController : MonoBehaviour {
      
          public Vector3 jump;
          public float jumpForce = 2.0f;
      
          public bool isGrounded;
          Rigidbody rb;
          void Start(){
              rb = GetComponent<Rigidbody>();
              jump = new Vector3(0.0f, 2.0f, 0.0f);
          }
      
          void OnCollisionStay()
          {
              isGrounded = true;
          }
          void OnCollisionExit(){
            isGrounded = false;
      }
          void Update(){
              if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
      
                  rb.AddForce(jump * jumpForce, ForceMode.Impulse);
                  isGrounded = false;
              }
          }
      }

This means that, when you leave the ground, isGrounded is set to false. I would also suggest a layers system, otherwise you can wall-jump.

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 d2tz · May 13, 2019 at 09:03 AM 1
Share

I'm currently facing this issue with the wall jumping. I looked at the layers documentation but couldn't figure out how it could help me stop the character wall jumping, do you $$anonymous$$d explaining a little bit more?

avatar image Akshat102 · Oct 18, 2021 at 10:38 AM 0
Share

I use all method but it is not working!

avatar image
0

Answer by Lormax23 · Aug 28, 2020 at 09:29 PM

@Arcane-Potato , I used your script to great success, I was able to get my player to move AND jump. It doesn't double-jump. The height of the jump can be very inconsistent though. I'm extremely new, so I was trying to figure out why. Since 'jump' and 'jumpForce' were set values, I thought it might be the Time.deltaTime, but when I removed that the player would jump sky high and never come back down. Any thoughts as to why the height of the jump is inconsistent?

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
avatar image
0

Answer by adityapatnaik_si · May 24, 2019 at 09:29 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Youjump : MonoBehaviour
 {
     public float speed;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {

      // Jump is an axis predefined in the Unity input manager
      // go to edit->Project Settings->Input and Check
           transform.Translate(0,speed*Input.GetAxis("Jump")*Time.deltaTime,0);
 
     }
 }
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
avatar image
0

Answer by Arcane-Potato · Aug 23, 2020 at 05:55 AM

   using UnityEngine;
   using System.Collections;
       
       public class PlayerController : MonoBehaviour {
       
           public Vector3 jump;
           public float jumpForce = 2.0f;
       
           public bool isGrounded;
           Rigidbody rb;
           void Start(){
               rb = GetComponent<Rigidbody>();
               jump = new Vector3(0.0f, 2.0f, 0.0f);
           }
       
           void OnCollisionStay()
           {
               isGrounded = true;
           }
           void OnCollisionExit(){
             isGrounded = false;
       }
           void FixedUpdate(){
               if(Input.GetKey("space") && isGrounded){
       
                   rb.AddForce(jump * jumpForce *Time.deltaTime, ForceMode.Impulse);
                   isGrounded = false;
               }
           }
       }

I know this post was made forever ago but just in case anyone else is looking at this in the future I found the above code to be extremely inconsistent on when it would detect my jump and also how high I would end up jumping so this is what I changed it too and it seems to be working for me so far. Just so you know you will have to increase the jump force a lot compared to what you had it set at.

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 mahnoorcodes · Feb 07, 2021 at 03:54 PM 0
Share

I added a RigidBody, and while I can jump, my cinemachine camera does funky things and bounce everywhere until I glitch into the void :/

avatar image mahnoorcodes · Feb 07, 2021 at 05:33 PM 0
Share

This script works. I get the Jump and Jump force options. But I cant jump for some reason? Space isn't working. Can you explain whats wrong please?

  • 1
  • 2
  • ›

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

22 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

Related Questions

Simple AI , glowing effect ?? 0 Answers

I have got bugs when i jump 0 Answers

Picking up collectables in a correct (custom set) order to open gates? 0 Answers

Platformer Jump Script Without AddForce 0 Answers

Make a rigidbody Jump (global up) 3 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