0
\$\begingroup\$

I have a character being moved using rigidbody.moveposition and I have an floating lowpoly island with a mesh collider. When my player walks into a steep slope or a wall, it passes right through. I assume this is because the mesh collider isn't thick enough but there must be a pretty simple fix. If anyone knows how to fix this it would be much appreciated.

Here's the code for the movement of my character.

private void Movement()
{
    if (AnimController.GetInteger("AnimPar") != 3)
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            walkSpeed = initialWalkSpeed * 2;
        }
        else
        {
            walkSpeed = initialWalkSpeed;
        }


        if ((Forward && Right) || (Forward && Left) || (Backward && Right) || (Backward && Left))
        {
            walkSpeed *= .7f;
        }

        if (Forward)
        {
            Delta += transform.forward * walkSpeed * Time.deltaTime;
        }

        if (Backward)
        {
            Delta -= transform.forward * walkSpeed * Time.deltaTime;
        }

        if (Right)
        {
            Delta += transform.right * walkSpeed * Time.deltaTime;
        }

        if (Left)
        {
            Delta -= transform.right * walkSpeed * Time.deltaTime;

        }



        rb.MovePosition(new Vector3(Delta.x, transform.position.y, Delta.z));
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I think you should use AddForce instead of SetPosition on your rigid body. Setting the position will place the body at the new position, but adding a force will cause the body to move through the simulation steps. Make sure you use continuous collision detection.

\$\endgroup\$
8
  • \$\begingroup\$ I'll try that when I get home but wouldn't that make the movement much less responsive? \$\endgroup\$ Feb 11, 2019 at 13:49
  • \$\begingroup\$ You can use a large force but you should adapt it to limit the speed of the character. \$\endgroup\$
    – Jay
    Feb 11, 2019 at 14:15
  • \$\begingroup\$ And how would you do that? Lower the force as the velocity increases? \$\endgroup\$ Feb 11, 2019 at 22:51
  • \$\begingroup\$ Would it be possible to send an example? \$\endgroup\$ Feb 11, 2019 at 23:56
  • \$\begingroup\$ Yes, you check the velocity and if it is low, use more force and if it is high use less force. What engine and language are you using? \$\endgroup\$
    – Jay
    Feb 12, 2019 at 2:13

You must log in to answer this question.

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