- Home /
issues with jumping and linecast2d and raycast2d
so I'm working on my first game, and the dynamics of it are a 2d sidescroller. So I need the player to be able to jump, and I'm trying to enable that by checking if the player is grounded using a raycast. The origin of the raycast is the position of a groundFinder point located at the player's feet. It casts a short ray downward (-Vector.up). I currently jump by setting the player object's y velocity. However, no matter how high I set the velocity, the player hits a "ceiling" at a certain point. I had this issue using AddForce as well, and also when I used a linecast instead of a raycast, and in that case I used a layer mask to only detect ground objects. In all of these cases, the jump height always maxes out at the same place. When I remove all ground-finding functions, the jump height reacts proportionally to the force/velocity added, even when I only press the jump key for a moment. Here is my current code:
//check for jumping if (inputY == 1) { RaycastHit2D hit = Physics2D.Raycast(groundFinder.position, - Vector3.up, 0.5f); if (hit) { rigidbody2D.velocity = rigidbody2D.velocity + new Vector2 (0, 1000000); } } I can never seem to make my code comments readable, but I hope you get the idea. As you can see, the ray cast has a length of .5, so the player can jump when he isn't really on the ground. Even when he jumps from a different height(a height above the ground) he reaches the same point he does when he is on the ground. I have tried to resolve this issue on my own without success, so any ideas or input of any kind would be appreciated. If you have any questions about more details of the project, I will answer them to the best of my ability.
"Vector2 (0, 1000000)" - that looks a bit high to me
$$anonymous$$ight be worth taking a short video with a service like http://www.techsmith.com/download/jing/ to convey what's going on.
What height does the player reach? It is 0.5 above the ground or higher?
Your answer
Follow this Question
Related Questions
unity2d issue with linecasts. 1 Answer
How to fix jumping from Unity2D tutorial? 2 Answers
Using Raycasts in order to move around 2 Answers
Unity ignoring bool grounded 1 Answer