• 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
1
Question by hamanieye · Aug 14, 2020 at 02:20 AM · scripting beginner

is there a way to make my Jump script only jump once?

this is my script[SerializeField] private float _Speed = 1.0f; private bool _IsGrounded = true; [SerializeField] private float _jumpForce = 2.0f; public Vector3 Jump;

 Rigidbody RB;

 private void Start()
 {
     transform.Translate(new Vector3(-22.80f, 1f, -24.17f));
     RB = GetComponent<Rigidbody>();
     Jump = new Vector3(0.0f, 2.0f, 0.0f);
 }

 private void OnCollisionStay()
 {
     _IsGrounded = true;
 }

 void Update()
 {
     PlayerMovement();
 }

 void PlayerMovement()
 {
     float hor = Input.GetAxis("Horizontal");
     float ver = Input.GetAxis("Vertical");
     Vector3 PlayerMovement = new Vector3(hor, 0f, ver) * _Speed * Time.deltaTime;
     transform.Translate(PlayerMovement, Space.Self);
    
     if (Input.GetKeyDown(KeyCode.Space) && _IsGrounded)
     {
         transform.Translate(Vector3.up * _jumpForce * Time.deltaTime);
         RB.AddForce( Jump * _jumpForce, ForceMode.Impulse);
         _IsGrounded = false;
     }


 }


}

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 Hellium · Aug 20, 2020 at 08:10 AM 1
Share

First thing to fix is that your OnCollisionStay method does not have the correct signature

  void OnCollisionStay(Collision collisionInfo)

1 Reply

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

Answer by Llama_w_2Ls · Aug 20, 2020 at 09:01 AM

Well you need to define _IsGrounded correctly. The way to do this is by creating an empty GO underneath the feet of your player and use Physics.CheckSphere to create a radius around your empty GO. Then you can say _IsGrounded = Physics.CheckSphere(Groundcheck, 0.4f, Ground); where Groundcheck is the empty GO under the player, 0.4f is the radius, and Ground is a public LayerMask that is the layer assigned to all walkable/jumpable ground. Then, _IsGrounded will be true when this sphere is colliding with an object with the layer ground, and will be false when not. This will ensure that once youve jumped, _IsGrounded is false and therefore you can't jump again. @hamanieye

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

141 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

Related Questions

Save different game object's datas using the same script? 3 Answers

Error In Lives While Respawning 1 Answer

How to detect right answer or not 0 Answers

What script should I use to make a countdown timer bar? 1 Answer

SteamVR loading screen not appearing at GameObject Transform? 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