Another one i’ve got over at UA that seems to be disappearing beneath the rest. Also, i’m really sorry for posting multiple questions so quickly after each other, I hope it’s okay. I’ll copy any answers back and forth to keep both Qs up to date.
I’m using the OnControllerColliderHit script from the docs to push rigidbodies around with no problems. What i’d now like to do is keep the initial jolt that I give an object (since the player will be running at speed), but thereafter slow my character’s movement speed down while he’s touching/pushing the object.
I can change the speed in OnControllerColliderHit, but I don’t have a way to reset it after the character stops touching the object. I tried using triggers, but then the character slows down just before he makes contact. Things got a bit messy when I tried using various booleans and/or distance checks. Below is the unmodified pushing script.
// this script pushes all rigidbodies that the character touches
var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
// body.velocity = pushDir * pushPower;
body.velocity = pushDir * moveChar.currentSpeed;
}