# Rigidbody collision problems

**URL:** https://discussions.unity.com/t/rigidbody-collision-problems/408003
**Category:** Physics
**Tags:** Scripting
**Created:** [February 25, 2010, 1:34pm UTC](https://discussions.unity.com/t/rigidbody-collision-problems/408003 "2010-02-25T13:34:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Disaster\_1](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@Disaster\_1](https://discussions.unity.com/u/Disaster_1)
#### Post date: [February 25, 2010, 1:34pm UTC](https://discussions.unity.com/t/rigidbody-collision-problems/408003/1 "2010-02-25T13:34:37Z")

</div>

Hi,

I am attempting to modify the FPSWalker script, and I’ve noticed that you can get ‘stuck’ against a wall mid jump if you keep moving against it. I’m not sure why this is happening as I am constantly applying gravity to my gameObject, but it seems to be overwritten by the movement force. Does anyone have an idea how I can ensure the object falls to the ground correctly when it moves against a ‘wall’ ?

Here is the code:

```auto
var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;

@script RequireComponent(Rigidbody, BoxCollider)

function Awake ()
{
    rigidbody.freezeRotation = true;
    rigidbody.useGravity = false;
}

function FixedUpdate ()
{
    if (grounded)
    {
        // Calculate how fast we should be moving
        var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        targetVelocity = transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;
       
        // Apply a force that attempts to reach our target velocity
        var velocity = rigidbody.velocity;
        var velocityChange = (targetVelocity - velocity);
        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
   
        // Jump
        if (canJump Input.GetButton("Jump"))
        {
            rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
        }
    }
   
    // We apply gravity manually for more tuning control
    rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
   
    grounded = false;
}

function OnCollisionStay ()
{
	grounded = true;
}

function CalculateJumpVerticalSpeed ()
{
    // From the jump height and gravity we deduce the upwards speed
    // for the character to reach at the apex.
    return Mathf.Sqrt(2 * jumpHeight * gravity);
}

```

---

<div class="post-metadata">

### Author: ![anon\_82095059](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anon\_82095059](https://discussions.unity.com/u/anon_82095059)
#### Post date: [March 23, 2010, 2:17pm UTC](https://discussions.unity.com/t/rigidbody-collision-problems/408003/2 "2010-03-23T14:17:45Z")

</div>

It is the character controller’s skin distance I think. Try lowering it in the inspector.

---

<div class="post-metadata">

### Author: ![anon\_78291153](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@anon\_78291153](https://discussions.unity.com/u/anon_78291153)
#### Post date: [March 23, 2010, 2:51pm UTC](https://discussions.unity.com/t/rigidbody-collision-problems/408003/3 "2010-03-23T14:51:26Z")

</div>

Is there a specific reason your using a rigidbody instead of a charactercontroller? If not try using the charactercontroller instead.

---

<div class="post-metadata">

### Author: ![anon\_82095059](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@anon\_82095059](https://discussions.unity.com/u/anon_82095059)
#### Post date: [March 23, 2010, 2:54pm UTC](https://discussions.unity.com/t/rigidbody-collision-problems/408003/4 "2010-03-23T14:54:19Z")

</div>

yes, it is better effected by physics (like explosions)
