Search Unity

trouble moving rigidbodies with OnControllerColliderHit

Discussion in 'Scripting' started by dacloo, Jun 10, 2006.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    This is a slightly modified version of the script in the docs.
    This script tries to also apply a torque to the rigidbody that was hit
    by the charactercontroller.

    Does hit.point contain the location of where I hit the rigidbody?
    (Currently the rigidbodies go beserk when I touch them)

    Thanks


    Code (csharp):
    1.  
    2. // Push all rigdibodies forward when running into them!
    3. function OnControllerColliderHit (hit: ControllerColliderHit) {
    4.     var body : Rigidbody = hit.collider.rigidbody;
    5.  
    6.     // Only rigid bodies
    7.     if (body) {
    8.         var normal = transform.TransformDirection(Vector3.forward);
    9.         //body.AddForce(normal * hit.moveLength * 3, ForceMode.VelocityChange);
    10.         body.AddForceAtPosition(normal * hit.moveLength * 3, hit.point, ForceMode.VelocityChange);
    11.         }
    12.     }
    13.  
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Have you tried exposing a force variable in the inspector by which you multiply the force. Possibly the force that gets a applied is simply too big.

    You could also try not multiplying by the hit.moveLength.