Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My NPC walks, but now how do I make him obey physics?

Discussion in 'Getting Started' started by edhalsim, Aug 3, 2023.

  1. edhalsim

    edhalsim

    Joined:
    Dec 19, 2015
    Posts:
    15
    I have an NPC that always turns and walks towards the player. All I'm doing in my script is setting an "isWalking" boolean and then turning the character in the proper direction. All of this works great; he doesn't float above the ground or slide.

    Now I want to introduce physics where he 1) doesn't walk through obstacles and, 2) if he goes up a slope, and falls off a cliff gravity takes over. I tried adding a rigid body to him, but then he started floating like he's on an elevator. Why? I have no idea.

    Any suggestions would be appreciated. Thanks for your time.
    - Ed
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
  3. edhalsim

    edhalsim

    Joined:
    Dec 19, 2015
    Posts:
    15
    Thanks I'll take a look. I really hope I can get the animation of the NPC walking correct because it works perfectly now with me just doing the rotation and setting the "isWalking" flag.
     
  4. edhalsim

    edhalsim

    Joined:
    Dec 19, 2015
    Posts:
    15
    Still no luck. I really don't care about path finding. I do need the NPC to be able to walk over small hills/slopes AND not walk through obstacles. I tried adding a capsule collider, but it didn't seem to do anything.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Are you using physics correctly?

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    For this to happen:

    Your enemy AI would need the same ground detection of a player and stop putting his movement inputs in while he is falling, then recover, etc.