1
\$\begingroup\$

I need a little help figuring out what's going on with an NPC's character controller. Here are the character controller settings for the NPC.

character controller settings The problem I'm running into is that the NPC isn't walking up a slight gradient as it should; instead, it moves through the ground.

Below are the settings for the ground.

ground mesh settings

The NPC isn't on the ground- it is a few units off. Any help is greatly appreciated.

Thanks in advance...

\$\endgroup\$
  • \$\begingroup\$ Do you have a collider and a rigidbody controller on your player? Do you have a rigidbody collider on your terrain? \$\endgroup\$ – Honeybunch Jul 29 '15 at 21:17
  • \$\begingroup\$ Yes, the 1st image is the NPC's character controller which has similar settings to the player's character controller. I tried using a rigidBody for the NPC but it was still doing the same thing. The image below is the the ground's mesh collider. Just to test, I added a kinematic rigidBody to the ground & the NPC is still behaving the same way. \$\endgroup\$ – Mark Laurel Jul 29 '15 at 23:25
  • \$\begingroup\$ Are you sure that both colliders fit exactly to the shape of the objects? \$\endgroup\$ – Vadim Tatarnikov Jul 30 '15 at 7:18
0
\$\begingroup\$

Use a raycast or this script. http://forum.unity3d.com/threads/groundnpc.181705/#post-1241872

Don't add a Rigidbody if you use the controller. It breaks it big time.

And check off convex on the mesh collider for your terrain. Try this first before adding a script. I remember having this problem before and that's all it took.

\$\endgroup\$
  • \$\begingroup\$ I updated & used the script from the link you posted. It works brilliantly! Thank you. \$\endgroup\$ – Mark Laurel Jul 30 '15 at 13:40
0
\$\begingroup\$

This is an update to a script found here:

This is a simple fix that prevents NPCs from walking through sloped mesh.

#pragma strict

var gravity = 20.0;
var verticalSpeed= 0.0;
private var collisionFlags : CollisionFlags;  // The last collision flags returned from 
                                              // controller.Move

function Update() 
{   
    ApplyGravity();

    // Calculate actual motion
    var movement = Vector3 (0, verticalSpeed, 0) + Vector3.zero;
    movement *= Time.deltaTime;

    // Move the controller
    var controller : CharacterController = GetComponent.<CharacterController>();
    collisionFlags = controller.Move(movement);
}

function ApplyGravity()
{
    verticalSpeed = IsGrounded() ? 0.0 : verticalSpeed - gravity * Time.deltaTime
}

function IsGrounded()
{
    var controller : CharacterController = GetComponent.<CharacterController>();
    return (controller.collisionFlags == CollisionFlags.Below);
}
\$\endgroup\$

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.