playMaker

Author Topic: Custom 2Dcontroller physics issue  (Read 124 times)

strawberries036

  • Playmaker Newbie
  • *
  • Posts: 5
    • View Profile
Custom 2Dcontroller physics issue
« on: September 12, 2019, 05:10:13 AM »
Hello I'm using Playmaker on my first game and it's awesome, I had a lot of help by reading this forum already!

My game is a platformer 2D and I would like to have the same feeling as Celeste or Meatboy. I started to handle the control of the player by using a Rigidbody2D but I noticed that the jump is inconsistent and the ground check not really good.

Here is one of my issues:



The collider in red is to small and when i hit the border of a platform I can't jump anymore because the ground is not checked.



If i make the ground check collider bigger it might touch the wall when stick to a wall and can jump infinitely.



I tried using raycast2d but I had the same issue. the Ray origin was in the wall due to Physics that allow a small overlapping between the Rigidbody and the collider.

I saw some nice tutorial to create a controller without using Unity physics but they usually don't really explain well the logic behind the code:

https://www.gamasutra.com/blogs/YoannPignole/20131010/202080/The_hobbyist_coder_1_2D_platformer_controller.php

https://www.youtube.com/watch?v=OBtaLCmJexk&list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz&index=2

My biggest issue is that I don't know how to use Raycast to set up the velocity of my controller. I have all the info with the Playmaker action but I'm lost when i need to use them.

Is it possible to combine as well Rigidbody with this Raycast solution?
OR to say that the raycast only detect a specific normal, so it will hit only the top edge of a boxcollider?

Thore

  • Sr. Member
  • ****
  • Posts: 305
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #1 on: September 12, 2019, 08:56:40 AM »
Hi,

I also work on a platformer, though nowhere near similar to Celeste. If you want a specific platformer and keep it at that, there are already scripts out there that do this nicely, for instance (this project is on Github, link at the video).


There are already several platformer-specific assets. I thought that the Rex Engine looks good for this.

I write this, because I am just barely through the thicket of core mechanics and I found that especially walljumping and ledgegrabbing are an absolute nightmare with PlayMaker (don't get me wrong, I love PlayMaker, but this is unusually difficult, but somehow easier with scripts). I was tempted several times to call it off and do the entire core with scripts. But I didn't do it, because I wanted to keep it open for expansion. But if I was making a platformer like Celeste, I would straight just buy a platformer asset, and use my time on building cool, and challenging levels.+

With that being said: here is how I do the ground checking:



An alternative I consider(ed) is using an overlap sphere. But for now this works for me.

I use a Capsule Collider 2D (with fixed z), and three rays. If you work with 2D physics, you must always use the corresponding 2D actions. In this case Ray Cast 2D. I use the following settings.

Almost everything none. Direction Y -1 for downwards. The others with x 1 (right) and -1 (left). My offset is x 0.3 and -0.3, and Y 0.1.  I store the Hit, and check every frame and use a Layer Mask. Simply set your ground or level geometry to this layer (called "Level" in my case), and have it check for that only.

Make sure that all of the rays return a different bool. E.g. Ground Center Detected, Ground Right Detected, Ground Left Detected. Then, use an action like Bool Any True, or Bool None True to combine the different bools into a clear "Grounded" bool.

To fiddle with the values, set a debug color and Debug to true. Put your character into the air. Set the Rigidbody2d to "static" and then play. This makes it so that you can tune the rays to your situation unti they fit nicely. And once you're satisfied, set the Rigidbody2D back to dynamic.
« Last Edit: September 12, 2019, 09:14:01 AM by Thore »

strawberries036

  • Playmaker Newbie
  • *
  • Posts: 5
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #2 on: September 12, 2019, 12:32:56 PM »
Hi Thore,
Thanks for your help!
I don't really want to purchase an asset because I would like to have a total control of the movements and I have already made some wall jumps and climbing mechanics with Playmaker.

I tried using your method and it was working pretty well until I saw that sometime the Raytrace2D can still hit a side wall. It happens on rare occasion but I think it depend of my initial velocity and make imprecise collision on 1 frame.I'll try to make the ray length shorter and shorter to avoid this issue but it could make the fix for the edge of the platform not working again.


Thore

  • Sr. Member
  • ****
  • Posts: 305
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #3 on: September 12, 2019, 02:23:14 PM »
You can also make the rays point all downwards, or only slightly outwards etc. :)

Edit: I also see another issue. The rays in your picture are perhaps a bit too outwards. You can easily move them a bit to the center. The point of them is to detect slopes . They are also probably too small. That's barely a pixel. You want them a little longer and only underneath your character.
« Last Edit: September 13, 2019, 03:51:12 AM by Thore »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14374
  • Official Playmaker Support
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #4 on: September 12, 2019, 10:26:01 PM »
Hi,

 You can also put some little trigger probes on each side and corners of your character, slightly outside its visual shape, and detection is then easier, with simple fsm on each of theses triggers to forward it to the main brain of your character ( the 'meta data' fsm for example).

Bye,

Jean

strawberries036

  • Playmaker Newbie
  • *
  • Posts: 5
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #5 on: September 13, 2019, 04:50:51 AM »
Thanks for your help.I didn't really understood what you mean Jean. My issue was the overlapping contact with the "ground checker" and the side walls. The "ground checker" was detecting ground on a side wall because of the tiny overlap collision between the rigidbody2D and the TileMap wall.But what you wrote about probes made me think about a new fix, and this one seems to work well for the moment.
It's simple and didn't think about it before!



I just made an other raycast2D on each side just for detecting side walls.



Then when this ray hit, it changes the length of one of the "ground checker Raycast", so this one will not detect ground any more.
I don't have any slopes in my game so this is working fine.
I let you know if I encounter any unexpected issues!

Thore

  • Sr. Member
  • ****
  • Posts: 305
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #6 on: September 13, 2019, 05:30:55 AM »
If you have no slopes in the game, then you only need to shoot the rays downwards. Put the side rays slightly inwards (i.e. not totally into the lower corners), and there should be no problem. It all comes down to tweaking those rays.

If you have slopes later, you just need to shoot the ray angled diagonally, but not as much as that they they exceed the sides of your collider.

strawberries036

  • Playmaker Newbie
  • *
  • Posts: 5
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #7 on: September 13, 2019, 11:46:26 AM »
Looks like it didn't fix anything in the end.
It's tried many different structures to make the FSM clearest as possible but in the end it look like the physic engine is too slow.
I don't unterstand why but the raycast is detecting collision too late.

And I tried to make a jump that is not affected by the frame rate ( yes the rigidbody is jumping more or less higher depending of the frame rate).
I used velocity2d instead of addforce2d and tried to set the velocity to zero when the jump was done.

I think these two problems are link, maybe it's something to do with fixed updates.
Maybe I should simulate the gravity by adding a constant velocity to the controller. but I still don't understand how to calculate collision with the Raytrace.

Thore

  • Sr. Member
  • ****
  • Posts: 305
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #8 on: September 13, 2019, 12:16:29 PM »
For a simple Jump, you need three states.

1) Listen to Button, if pressed...
2) Check If Grounded, and if true...
3) Add Force 2D

You only need more when you have different jump states, cooldown, hangtime and whatnot.

If that doesn't work, something is borked. My guess is that you Set Velocity 2D elsewhere (Movement FSM?), and you overwrite the Y velocity, and that's why jumping is not applied.

I suggest you ignore the grounded check for a moment and first get the jump to work. If you can jump (also in mid-air), and that works, you can re-add the Ground check again).

strawberries036

  • Playmaker Newbie
  • *
  • Posts: 5
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #9 on: September 14, 2019, 02:46:57 AM »
So to be sure there was not any piece of code making this issue, I started with a fresh new scene.

There is nothing except a the player and the ground, 2 FSM for registering the A input, and the Jump system.



The listener is just a bool test on the press A button.
When A is pressed, the velocity is set to 0 to be sure we start from scratch then set velocity at 5 for 0.25 sec in realtime (I use this because later I need to make a jump that is jumping a bit higher if you hold the button, and it's not something I could do with an addforce2d)

So this is just basic and should work perfectly but:


The Jump should stop just on the top of the edge of this line but goes a bit higher sometime ( usually when  I have many windos open in the editor and checking the fsm to debug)



And a bit shorter sometime (when everything run smoothly and no window is open in the editor)

So I guess the rigidbody or the gravity is not calculated with fixed update.
This is a big issues when you need to do really precise jumps.

Thore

  • Sr. Member
  • ****
  • Posts: 305
    • View Profile
Re: Custom 2Dcontroller physics issue
« Reply #10 on: September 14, 2019, 04:47:52 AM »
You can open the actions by pressing the gear icon in their upper right corner > Edit Script. But AddForce2D is applied in FixedUpdate.

I found this case:
https://forum.unity.com/threads/solved-inconsistent-jumping-height.515369/