Question by 
               cbholmes03 · Feb 26, 2018 at 04:20 AM · 
                c#oncollisionenteroncollisionexit  
              
 
              C#: How can you make your character jump only when you are touching the top of the object?
I have tried looking at a lot of other questions that are similar but all of them have the same problem: that you can jump up the wall. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class Movement : MonoBehaviour {
     public float movementSpeed = 20f;
     public float jumpHeight = 1250f;
     private Rigidbody2D rb2d;
     private bool isJumping = true;
     void Start ()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
     private void FixedUpdate()
     {
         float move = Input.GetAxis("Horizontal");
         Vector2 movement = new Vector2(move * movementSpeed, 0f);
         rb2d.AddForce(movement);
     }
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             isJumping = false;
         }
     }
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             isJumping = true;
         }
     }
     void Update ()
     {
         if ((Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) && !isJumping)
         {
             rb2d.AddForce(Vector2.up * jumpHeight);
         }
     }
 }
Is there any other way I can use the OnCollisionEnter2D to specify what side it collides on?
               Comment
              
 
               
              Answer by SwedishDerp · Feb 26, 2018 at 07:41 AM
I suggest you simply cast a raycasthit downwards with a range of, for example, 1 and when the ray hits an object with the tag "Ground" it will enable the isJumping and vise versa. It's more flexible and efficient for these kind of systems :)
It should work with any collider as long as the ray is casting downwards! :)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                