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 Overender · May 28, 2020 at 05:10 AM · 3djumpjumpingnewbienew user

Jump Script Not working (3D)

I've been trying to get this C# jumping script to work, but I can't. The script was originally for a 2D game, but the only thing I changed was turning 2D Rigidbodies into 3D ones and replacing the Overlapcircle used for checking if the player is grounded with a sphere. The compiler and the engine both say the script is alright, but when I press the jump key, nothing happens.

 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.Security.Cryptography;
 using UnityEngine;

 public class PlayerJump : MonoBehaviour
 {
 
     public float jumpForce;
 
     private bool isGrounded;
     public Transform feetPos;
     public float checkRadius;
     public LayerMask whatIsGround;
 
     private Rigidbody rb;
 
     // Update is called once per frame
     void Update()
     {
         isGrounded = Physics.OverlapSphere(feetPos.position, checkRadius, whatIsGround).Length > 0;
 
         if (isGrounded == true && Input.GetKeyDown(KeyCode.Space));
         {
                 UnityEngine.Debug.Log("jump");
                 rb.velocity = Vector3.up * jumpForce;
         }
     }
 }


Thanks in advance :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SteenPetersen · May 28, 2020 at 05:13 AM

I looked through your code and it seems you need to make sure you get ahold of the rigidbody. and you need to make sure that you 'checkRadius' is sufficient. I have refactored your code, perhaps it can help you understand it a bit better.

Also, it is not necessary to be checking for grounded all the time, only when you need it, so I made property for it.

I have furthermore added an OnDrawGizmos so that you can see if your check radius is enough to actually check for the ground.

 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.Security.Cryptography;
 using UnityEngine;

 // make sure this GameObject has a rigidbody as it is necessary for this script to work
 [RequireComponent(typeof(Rigidbody))] 
 public class UnityAnswers : MonoBehaviour
 {
     [SerializeField] float jumpForce;
     [SerializeField] Transform feetPos;
     [SerializeField] float checkRadius;
     [SerializeField] LayerMask whatIsGround;
     private Rigidbody rb;
 
     public bool IsGrounded
     {
         get { return (Physics.OverlapSphere(feetPos.position, checkRadius, whatIsGround).Length > 0); }
     }
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>(); // we know this will always work because at the top we have required this component
     }
 
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if(IsGrounded)
                 rb.velocity = Vector3.up * jumpForce;
         }
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.DrawWireSphere(feetPos.position, checkRadius);
     }
 }


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 Vighnesh01 · Jun 20, 2021 at 02:57 PM

Hey there , if anyone has the same problem. Try to change the layer to "Ground". Once I made a prototype everything was working fine. Then I changed my assets with new models and didn't set the layer as Ground.

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

171 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 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 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 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 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 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 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

How do I make my character jump? 1 Answer

Not able to jump 1 Answer

Jumping not always work 2 Answers

I cant make my character jump 1 Answer

How to position the jump animation with the object's body? 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