#Prevent "Pushing" On Rigidbody2D

1 messages · Page 1 of 1 (latest)

frank forge
#

I've been searching for a few hours now to no avail in trying to find a workable solution. (Also wanted to note that I'm very new to gamedev)
I'm making a 2D Multiplayer Platformer, and I would like to prevent players from being able to push each other.

Currently using Unity 6000.3.10f1 with the use of MirrorNetworking and UnityInputSystem
This is the closest I've seen to a solution, but unfortunately I found out that AINavmesh doesn't work for 2D https://community.gamedev.tv/t/how-to-make-rigidbody-gameobjects-block-each-other-without-being-able-to-be-pushed-by-others/90640

End Goal:

  • Players using Rigidbody2D + Box Collider2D
  • Players can't push each other
  • Players can collide
  • BoxCollider2D platforms (floors/walls) that cannot be moved
  • BoxCollider2D objects with Rigidbody2D that can be pushed by players/other rigidbodies

Currently (See video for demonstration):

  • Players using Rigidbody2D + Box Collider2D
  • Players can push each other

(I'll send my player script in a moment since it can't send it in one message)
Please let me know if I should post this somewhere else or if you need any more information

amber wraith
#

Making this networked is going to be kind of a nightmare too.

#

hard enough without the networking

frank forge
#

yeah lol

#

Just spent a day getting animations to be synced

#

We can ignore the networking for now if thats at all helpful

amber wraith
#

I struggled for a while in 3D with the opposite - allowing networked multiplayer players to be able to partially push each other fluidly

#

The first thing I would probably try is basically a boxcast based approach

#

if you see another player - adjust your own velocity such that you will stop just short of colliding with them

#

I will again stress that the networking is going to make this about 10x more difficult if you want it to feel good.

frank forge
#

I believe ya, have spent about 3 weeks so far on this project and its all just been the lobby

#

I believe this is correct? (Just waiting for my scene to load)

playerInfront = Physics2D.BoxCast(transform.position, boxCol.size, 0, rb.linearVelocity, 0.1f,LayerMask.NameToLayer("Player"));
amber wraith
#

NameToLayer is not the way to create a layer mask

frank forge
#

ah..

amber wraith
#

the best way to make a layermask is by assigning it in the inspector

#

public LayerMask myLayerMask;

frank forge
#

Thanks

amber wraith
#

it will show up in the inspector and you can set it there

#

the second best way is with GetMask - LayerMask.GetMask("Player")

frank forge
#

How do I make a size accurate gizmo for this? I was initially using a box cast for my grounded detecting, but my gizmo wasn't giving accurate information

#

Now that I'm thinking about it, it's probably because I was just drawing a cube gizmo, but since its a raycast it actually "moves" so the size is bigger than the box collider

frank forge
#

Been trying to figure it out on my own but I don't understand how boxCasts work..
Should I either physically transform the side its casting from depending on which way the player is facing
or figure out how to use the minDepth and maxDept of the BoxCast

amber wraith
frank forge
#

okay

amber wraith
#

a boxcast is like throwing a box in a straight line through the scene

#

if it hits anything it will report those hits

frank forge
#

right, so I need to set the start position and direction based on which way the player is facing

amber wraith
#

sure but that will typically just be player.transform.right

frank forge
#

i dont see a right definition under my gameobject transform? I'm probably misunderstanding sorry

amber wraith
#

wdym by that

#

it's definitely there

frank forge
#

ooh the rigidbody gotcha

amber wraith
#

no.. the Transform

frank forge
#

sorry I was using transform.position instead of rb.transform.position.right

amber wraith
#

it's neither of those

frank forge
#

im very confused

amber wraith
#

position is not involved

#

why are you putting position

frank forge
#

oops old code. Thats my bad

#

Is there a way to visualize this with gizmos? I'm not sure if I'm setting the parameters correctly

frank forge
#

I'm sorry, I get that I'm being very frustrating. I promise I am trying my best to research/test things on my own and understand before I ask

This is what I have right now

        void Update()
        {
            if (!isLocalPlayer) return;
            animator.SetBool("isMoving", rb.linearVelocityX != 0);
            animator.SetBool("Y", rb.linearVelocity.normalized.y != 0);

            stopPush = Physics2D.BoxCast(transform.position, boxCol.size*transform.localScale, 0, rb.transform.right);
        }

        void OnDrawGizmos()
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireCube(transform.position, boxCol.size*transform.localScale);
            Gizmos.DrawRay(transform.position, transform.right);
        }

Current issues:

  • the ray does not switch positions (its still on the yk.. right) no matter what direction I am walking/facing because I am not physically rotating my character, I am editing its linearVelocity (I do have a floatMoveDirection that contains the player's x as a negative/positive value)
  • stopPush is always true because its casting from inside the player hitbox which is why I asked how to change which side/position of the ray
#

I was able to amend the first issue, but I still need to figure out how to either ignore the player's own hitbox or cast outside of it in the correct direction

amber wraith
frank forge
amber wraith
frank forge
#

Would you be willing to elaborate on either both methods or the one you recommend?

amber wraith
#

the second one will be much simpler

#

just:

int oldLayer = gameObject.layer;
gameObject.layer = someNewLayer;
// Then do your boxcast, with a layermask that excludes the new layer
gameObject.layer = oldLayer;```