1
\$\begingroup\$

I'm making a prototype for a game idea but hitting a problem :(

Quite new to Unity and feel like this should be relatively easy to solve, but keep going around in circles, and hoping to get some help :)

Video: https://gfycat.com/candidplushemeraldtreeskink

GameObjects:

  • Red box: has BoxCollider2D
  • Duck: has BoxCollider2D, Rigidbody2D (dynamic), some scripts
  • Wall: has BoxCollider2D, Rigidbody2D (dynamic), some scripts

Rules:

  • Red box: doesn't move, everything should stop when colliding with it
  • Duck: moveable by player, can't push "Wall"
  • Wall: moveable by player, can push "Duck"

To get "Wall can push Duck, but Duck can't push Wall" working, I set the Mass for Wall to be Infinity. However, this seems to have a side effect where Wall can now push Duck through Red box. Note: even increasing mass to something like 500 has this side effect too

If the mass for Wall is smaller, then it won't push Duck through Red box. However, with the smaller mass, Duck can then push Wall (which I don't want).

The script for moving Wall is:

private void FixedUpdate()
{
    // https://learn.unity.com/tutorial/world-interactions-blocking-movement?uv=2019.2&projectId=5c6166dbedbc2a0021b1bc7c#5ce3cdabedbc2a3ce61754e8
    Vector2 position = rigidbody2d.position;
    position.x += speed * horizontal * Time.deltaTime;
    position.y += speed * vertical * Time.deltaTime;
    rigidbody2d.MovePosition(position);
}

Question:

  1. What should I do so "Wall can push Duck, but Duck can't push Wall" holds true, without Duck being pushed into the Red box.
  2. Should I be using Dynamic Rigidbody (sort of physics with collisions and pushing), or Kinematic Rigidbody (and handle collisions and pushing manually)?

Thanks in advance!

\$\endgroup\$
4
  • \$\begingroup\$ Have you tried using a kinematic body for the wall, with your existing setup? This is the usual route to make an object that can push but not be pushed. \$\endgroup\$ – DMGregory Aug 8 '20 at 11:49
  • \$\begingroup\$ Just gave that a go and similar results, the Duck hits RedBox, jitters a bit and gets pushed/squeezed out of the way (the overlap with RedBox is bit different, but similar problem). Also, the Kinematic wall goes straight through the RedBox. \$\endgroup\$ – pyko Aug 9 '20 at 12:48
  • 1
    \$\begingroup\$ Ah, so you do want duck to push wall, enough to keep a duck-sized space between the static red box and the wall. In that case dynamic bodies are indeed the way to go. You can stop duck from moving into wall by using shape casts to detect where a collision would occur, and stopping just before \$\endgroup\$ – DMGregory Aug 9 '20 at 15:32
  • \$\begingroup\$ Ah, I see, so by preventing the Wall from moving further, the Duck is actually pushing the wall to prevent movement! In my mind the Duck not being able to move the wall meant it doesn't push it, but this way of thinking also makes sense. Will have a look at using Shape Casts. Thank you! \$\endgroup\$ – pyko Aug 10 '20 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.