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 andrewwebber25 · Sep 20, 2017 at 01:07 AM · colliderjumpinggroundedground detectionis trigger

How to only Jump on Ground Items

So im making a 2D platformer and im having an issue with jumping. I just started the game so there isnt much code. I got all the code and animation for moving and jumping sorted out but my "groundCheck" isnt working properly. I made an empty game object and put it on the feet of my Player. The code i attached to it is called "GroundCheck" and it is supposed to restrict the Player from being able to jump unless they are on the "ground" and only jump one time (not infinitely).

I think this is a pretty normal issue but my Player can jump on anything that has a collider including coins and enemies. For my actual ground platforms, I put "Ground" as their layer at the top of the inspector. My question is, how do I decipher between what is the difference between the ground, a coin or an enemy in a way my code will understand? I think thats the problem. The groundCheck Is Trigger so I feel like every collider it is coming into contact with is somehow being considered ground? Ill Post my code for the groundCheck and the Player its attached to. Let me know if you guys need anymore info! Thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public float maxSpeed = 3; //limits the speed 
     public float speed = 50f;
     public float jumpPower = 150f;
 
     public bool grounded;
     private Rigidbody2D rb2d;
     private Animator anim;
 
     void Start () 
     {
         rb2d = gameObject.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
     }
     
     void Update () 
     {
         anim.SetBool("Grounded",grounded);
         anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
 
         if (Input.GetAxis("Horizontal") < -0.1f)
         {
             transform.localScale = new Vector3(-1,1,1); //These two bits of code make the player animations switch directions when walking back a forth
         }
         if (Input.GetAxis("Horizontal") > 0.1f)
         {
             transform.localScale = new Vector3(1, 1, 1);
         }
         if(Input.GetButtonDown("Jump") && grounded ==true)
         {
             rb2d.AddForce(Vector2.up * jumpPower);//Jumps with spacebar (spacebar is Jump by default)
         }
     }
     void FixedUpdate()
     {
         Vector3 easeVelocity = rb2d.velocity;
         easeVelocity.y = rb2d.velocity.y; //This is set up to not affect y axis
         easeVelocity.z = 0.0f; //set for 0 cause you dont use z axis in 2D game
         easeVelocity.x *= 0.75f;
 
         //for left and right arrows
         float h = Input.GetAxis("Horizontal");
 
         //Fake friction / keeps player from sliding
         if(grounded)
         {
             rb2d.velocity = easeVelocity;
         }
 
         //will physically move player when left / right pressed
         rb2d.AddForce((Vector2.right * speed) * h);
 
         //basically if this hits max speed, it will keep it there and not go faster
         if (rb2d.velocity.x > maxSpeed) 
         {
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);//this is max speed for moving right
         }
         if(rb2d.velocity.x <-maxSpeed)
         {
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);//this is max speed for moving left
         }
 
     }
 
 }


And for the groundCheck

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GroundCheck : MonoBehaviour {
 
     private Player player; //refferences the Player script
 
     void Start ()
     {
         player    = gameObject.GetComponentInParent<Player>();//If this code messes up what is considered grounded, on ep 3 there is a comment with other code you can try
     }
     
     void OnTriggerEnter2D(Collider2D col)
     {
         player.grounded = true;//NOTE: This may be the bad code. this makes it look like if the grounded game object under the players feet collides collides with another Collider 2D, he is grounded. This wouldnt be true for landing on enemies.
     }
     void OnTriggerStay2D(Collider2D col)
     {
         player.grounded = true; //Fixes groundCheck bug?
     }
     void OnTriggerExit2D(Collider2D col) 
     {
         player.grounded = false;//When player is not on a collider, he is not grounded (i dont think this should always be false)
     }
 }
 
 

   

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IndievdB · Sep 20, 2017 at 01:58 AM

Normally you would check the Collider2D's layer with col.gameobject.layer == "Grounded", but I don't recommend this. You will have to keep track of everything you collide with and using a collider to check if you are grounded can be a little inconsistent. I suggest you ditch GroundCheck.cs and use raycasts instead. Something like this should work:

 bool IsGrounded()
 {
     return Physics2D.Raycast (transform.position, new Vector2 (0, -1), groundCheckDistance, LayerMask.GetMask ("Ground"));
 }

In your update, you can use Debug.DrawRay to visualize the raycast.

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

83 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

Related Questions

Making a sphere jump on specific surfaces only 4 Answers

Player does not jump when running at the edge of a ledge (How to add jump tolerance?),Player Does not jump when at the edge of a ledge 1 Answer

Grounded check not working on sloped ground. 1 Answer

Player can jump on non ground items 1 Answer

How to jump on only "ground" colliders 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