1
\$\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\$
  • \$\begingroup\$ Do you want your player to collide with NPCs or just go through them? \$\endgroup\$ – John Hamilton Jun 9 '17 at 16:56
  • 1
    \$\begingroup\$ @JohnHamilton Collide, hence the issue :) Otherwise Id just ignore its Layer \$\endgroup\$ – Majs Jun 9 '17 at 17:25
  • \$\begingroup\$ Do you want the NPC to be affected by any other objects? \$\endgroup\$ – ChargerIIC Jun 9 '17 at 20:59
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\$
  • \$\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 '17 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\$ – John Hamilton Jun 9 '17 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 '17 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 '17 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\$ – John Hamilton Jun 9 '17 at 22:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.