- Home /
Pushing rigidbodies around
Related to my movement question (which looks to be resolved by Mike, thanks), but it's different enough to warrant its own question.
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;
}
Anyone have any ideas? Thanks
Answer by straydogstrut · May 22, 2010 at 01:38 PM
Response from Andeeee on the Unity forum. I'm ashamed I didn't think of this.
The CharacterController's Move function returns a CollisionFlags object. This detects whether an object is in contact with the top, sides or base of the controller. If you check this in the Update/FixedUpdate function, you can tell if the object has stopped touching another rigidbody and then restore its speed to normal.
Hmm, I'm thinking pasting this answer - and the one about NPC's rotating around a waypoint - over here may be doing more for my rep points than Andeeee's? Feel free to replace these answers yourself Andeeee. Thanks again.
Your answer
Follow this Question
Related Questions
Rigidbody speed in some senes different?! 1 Answer
Rigidbody regenerated acceleration movement ? 0 Answers
How to limit the force from player input without limiting speed 2 Answers
How to make an object without Rigidbody move at the same speed as an object with Rigidbody? 1 Answer
When I use rigidbody.Moveposition, the object doesn't move accurately. 1 Answer