• 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 /
This question was closed Sep 20, 2016 at 10:45 PM by Refoxy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Refoxy · Aug 30, 2016 at 03:13 AM · c#collisiongameobject3doncollisionenter

Jump on the ground

Hello! I know that a lot of these questions have already been asked, but I have looked through many, and none of them seem to work for me. My problem is: I want my player to jump only when he is on the ground.

I'm new with scripting, i'm really a beginner. The original code is from the unity tutorial "roll-a-ball".

Sorry for my bad english, and here is my code, any help is appreciated!

 using UnityEngine;
 using System.Collections;

 public class Movimento : MonoBehaviour {

 public float speed;
 public float jump;
 private Rigidbody rb;


 void Start ()
 {
     rb = GetComponent<Rigidbody>();
 }

 void FixedUpdate ()
 {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");

     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);

     if (Input.GetKeyDown(KeyCode.Space)) {
         GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0)); }

     rb.AddForce (movement * speed);

     }

 }
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

  • Sort: 
avatar image
0

Answer by sparkzbarca · Aug 30, 2016 at 03:45 AM

there are multiple ways to do this.

you can cast a ray from the center of the player object or the player objects attached collider "down" that is the local down not global down and do a distance check. Basically if the player is say 6 feet tall so the center is 3 feet then if your 3.1 feet or less from the ground allow a jump, if not, don't.

if that seems too complicated this is really the same thing but it hides writing the code for you.

Attach a sphere collider to your player and check the is_trigger box. then make it just big enough so the bottom edge of the collider is touching your players feet. then for the collider do the code

 collider ontriggerenter (collider other)
 {
 if other.gameobject == terrain
 (then your touching the terrian because its inside the sphere)
 //allow jumping
 }

that's not foolproof by the way. If your falling very close to the edge of the cliff you could be allowed to jump because you have terrain in your sphere. This is just to help you get started.

Comment
Add comment · Show 1 · 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 hellopeople0004 · Aug 30, 2016 at 04:01 AM 0
Share

A better way to do that method would probably be to use a capsule collider or a cube collider as a trigger, It would [hopefully] fix the issue probably.

avatar image
0

Answer by hellopeople0004 · Aug 30, 2016 at 03:57 AM

Try adding this to your script:

 void  OnTriggerEnter (Collider other){ 
       isGrounded = true;
   }
 void  OnTriggerExit (Collider other){ 
       isGrounded = false;
   }

(Make sure you add isGrounded as a variable to your script) Then where your jump code is, check if isGrounded is true.

 if(Input.GetKeyDown(KeyCode.Space)) {
     if(isGrounded) {
         GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0)); 
     }
 } 

What this [should] do/es is detect if the player is colliding with any object, and if it is then isGrounded is true, allowing you to jump. When you aren't touching anything (e.g in air) then you cannot jump. Hope this helped! Let me know if there are any errors, I did this in JavaScript and converted it to CS so I cannot guarantee there will be no errors (but there shouldn't be any.)

1 Issue however, if you are touching a wall, you will be able to repeat jumping. To fix, just change the first code to:

 void  OnTriggerEnter (Collider col){ 
       if (col.tag == Ground) {
           isGrounded = true;
       }
   }
 void  OnTriggerExit (Collider col){ 
       isGrounded = false;
   }

And then set your terrain / ground's tag to "Ground" Again, let me know if there are any issues :)

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer

WALL collision detection problem... help!!!! 0 Answers

How do i get the LocalSize of an object? 2 Answers

Collision issue with SetParent (C#) 1 Answer

Issue with GameObject has been destroyed but it is not destroyed 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