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 NatsumeKita · Jun 12, 2017 at 09:16 AM · uijumpingground detection

calling jump as a ui button function, how to detect ground within the function?

Thank you everyone in advance for any help. I am self learning to code and use unity. I have the following simple jump function.

 using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;

      public class JumpButton : MonoBehaviour {



 public float jumpSpeed;

 private Rigidbody rb;
 private int count;
 //Adding the boolean variable onPlane to check if object touching the ground tag
 private bool onPlane;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
     count = 0;
     //make onPlane true
     onPlane = true;

 }
 void FixedUpdate()
 {
     
     //If onPlane true you can jump 
     if (onPlane)
     {
         if (Input.GetKey(KeyCode.Space))
         {
             rb.velocity = new Vector3(0f, jumpSpeed, 0f); 


             onPlane = false;
         }
     }

 }
 //after the object touches the floor again, make onPlane true(I think I need to replicate this in the function somehow?)
 private void OnCollisionEnter(Collision collision)
 {
     //My plane is actually tagged Ground
     if (collision.gameObject.CompareTag("Ground"))
     {
         onPlane = true;
     }
 }
 public void canjump()
 {

//I can use an if statement here to check onPlane state.

          if(onplane)

{

         rb.velocity = new Vector3 (0f, jumpSpeed, 0f); 

} onPlane = false;

//I think that I need another if statement of sorts here, but am not sure what I can do exactly }

 public void cannotjump(){
     

 }


}

This is attached to my player character, and the ui button is linked and calling the function. I can jump fine, but I can not jump again after landing. (Not my intention) I know that setting the on plane to fall will disable the jump, but how to I re-enable it within the same function after the player has again landed on the plane? I have tried so many ways the past two days, going crazy lol.

Any help would be much appreciated, can't get my head around this one for some reason, maybe lack of sleep. Only been using unity for 4 months, this is my 4th project. Going to kick myself probably if the answer if something simple.

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 oscarlundberg · Jun 12, 2017 at 10:11 AM 0
Share

This question is a little messy so it's hard to read out what the problem is... First things first though you have if(onplane) and not if(onPlane) in your canjump method.

Check if that's the problem and/or the only problem and edit the question so it's a little more clear!

0 Replies

· Add your reply
  • Sort: 

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

ground check stops working after one jump,im making an fps and i added a movement scrip and a ground checker script, if i press the jump button and then fall, the ground checker stops working. 0 Answers

Raycasting bug when checking if player is grounded 2 Answers

Jump if ground is detected 1 Answer

Player can jump on non ground items 1 Answer

Grounded check not working on sloped ground. 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