Trying to make a Pushable KinematicBody2D meant to be pushed by Player and AI companion

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EyeBallTank

I’m making a 2D platformer game where the player accompanies an AI character following them, while also having some commands for said AI character.
A situation i’m stuck at is figuring out a pushable object that both the Player and Companion (The AI character) can push, which also affects their moving speed.

The 3 relevant scenes are:

  • PlayerCopy
  • CompanionCopy
  • PushableCopy

(Copy being used at the end, because i copied these scenes so i could experiment new ideas on seperate versions).

I used KinematicBody2D instead of RigidBody2D for 2 reasons:

  • Every video i watched about pushing RigidBody2D almost used the same “velocity = move_and_slide(velocity, Vector2.UP, false, 4, PI/4, false)” method and using that in my Player character script always messed with their values/physics (They jump higher and move faster than they should be).

Because the way i see it, infinite_inertia seems to be the one parameter i want to modify but it’s not easily accessable due to how the move_and_slide parameters are set up (Might be wrong here).

  • The “pushable” object still felt floaty (But this might just be some settings i could’ve changed a bit more, because the in-universe context of the pushable is a large vase that should feel heavy).

So as i used a KinematicBody2D instead, i went through different methods:

  • Used a state machine on the Player and Companion, including a PUSH state.

  • Also put a state machine on the Pushable object, with “FROZEN” and “PUSHED” states.

  • Once made it so that if the player touches the Pushable, they switch to a “state” with slow speed (Making the Pushable almost like a sludge hazard)

And this is if the player walked on top, which was done just to see if it worked.

  • Gave the Pushable extra CollisionShape2D nodes based on up/down/both left and right sides, with a “left and side” collision being under a “pushableside” group.

One time it was 3 collisions, the other it was 4. I scrapped this idea and went back to just one CollisionShape2D.

  • Used a RayCast2D on the player but it had like 3 problems:
  1. It’d crash with things like the Tilemap or Area2D scenes because of specific code expecting another KinematicBody2D.

  2. It didn’t flip properly (And even with code to flip, the angels were random as if it wasn’t properly counting 360 degrees).

  3. The Raycast, no matter the size, would stop pushing the Pushable if the player was high enough in terms of placement.

The idea of using a RayCast2D came from a video by clecioespindolagamedev (Video titled “Como EMPURRAR e RESPAWNAR OBJETOS na Godot 3.4 (+Bug Fix Plataforma)”) and the video even showed the pushable object as a KinematicBody2D.

Because i believe it’s possible to make a pushable object (And pushing state for the player) work while using a KinematicBody2D instead of RigidBody2D (And not just with a Player character but even using an AI character, having more than one “actor” to push the object).

My project can be seen here:

Admittedly, my skills are still poor and i’ve committed the error of copying different tutorials and trying to “Frankenstein” mix things up.

You can even see the amount of commented code that i used and sometimes occasionally uncomment just to try something.

But i hope that some ideas/methods here ring a bell to someone.

An idea i’ve thought of trying (“Thought” being the keyword, because i don’t know exactly how) is:

  • Pushable secretly uses “movement inputs” like the Player but only if Pushable and Player are colliding.

  • This if statement/condition involves the Pushable detecting if the Player is colliding with its left/right sides specifically.

  • This also must work with the Companion, presumably leading to the pushing speed being increased a little.

Even then, secretly adding movement controls to the Pushable could lead to new problems.

Either way, any help is appreciated, even if i should practice a little more or learn things properly.

I finally fixed this:
I used this video as reference and stored the Player/Companion code in a function called “pushcheck” then used said function in their MAIN states.

EyeBallTank | 2023-03-01 15:55