2
\$\begingroup\$

I have a player, and several NPCs. I also have several static colliders in the world (trees, water, fence, etc), I want my NPCs to collide with this, so the Rigidbody2D needs to be dynamic, since kinematic does not collide with static colliders (colliders without a rigidbody).

My problem is that since both player and NPC have dynamic rigidbodies, they push each other when they collide. I could disable this by setting the following in the NPC script:

void OnCollisionEnter2D(Collision2D other){
    if (other.gameObject.name == "Player") {
        isMoving = false;
        myRigidBody.isKinematic = true;
    }
}

However, the NPC still gets pushed a few pixels before it stops, its barely noticable, but it is noticable, and if you keep colliding with the NPC you could move it as far as you wanted (tedious, but possible).

I've spent a few hours looking and cannot find a good solution to this. I want my NPC to collide with static colliders, but I don't want them to get pushed by the player. How can I solve this?

\$\endgroup\$
3
  • \$\begingroup\$ Do you want your player to collide with NPCs or just go through them? \$\endgroup\$ Jun 9, 2017 at 16:56
  • 1
    \$\begingroup\$ @JohnHamilton Collide, hence the issue :) Otherwise Id just ignore its Layer \$\endgroup\$
    – Majs
    Jun 9, 2017 at 17:25
  • \$\begingroup\$ Do you want the NPC to be affected by any other objects? \$\endgroup\$
    – ChargerIIC
    Jun 9, 2017 at 20:59

1 Answer 1

0
\$\begingroup\$

This one is actually pretty easy to do (although it may not be apparent at first look). All you need is a massive NPC. Not in terms of size but in terms of mass.

Just set the Mass parameter under the Rigidbody to something like 1000 times the player's mass. That way, your player will never be able to apply enough force to the NPC, but you can still make the NPC move, collide, be affected by physics etc.

Demo gif: enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ This is the first thing I tried. However it still gives a tiny push before setting the Mass. If you tried setting mass in the OnCollisionEnter2D(), I think ud get the same result \$\endgroup\$
    – Majs
    Jun 9, 2017 at 17:53
  • \$\begingroup\$ @Majs Then you need a bigger Collider2D to do detection only. Set it to trigger (which won't act as a rigidbody collider) and let it do magic for you. docs.unity3d.com/ScriptReference/Collider2D-isTrigger.html \$\endgroup\$ Jun 9, 2017 at 18:46
  • \$\begingroup\$ Can you explain to me how this works? I create a child GameObject of my NPC, I attach a Collider2D to it and set it to trigger. I can now add the OnTriggerEnter2D() to a Script attached to my NPC, and it works? How does it know that I want to use the collider from the child object? Since that one is a trigger and the one on the NPC is not? \$\endgroup\$
    – Majs
    Jun 9, 2017 at 20:39
  • \$\begingroup\$ Btw it works, just wonder how/why it works. Shouldnt I have to attach a Script to the child where I check OnTriggerEnter2D? \$\endgroup\$
    – Majs
    Jun 9, 2017 at 20:40
  • \$\begingroup\$ @Majs I'm not sure how it knows, I'd have to really dig in to find out. I think that's a solid question you could ask here, I'd upvote that and try to give it exposure here. \$\endgroup\$ Jun 9, 2017 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .