- Home /
Object push character controller
I've tried to research this myself, but getting quite confused with all the code that's floating around. I want that box which is animated to move "side-to-side" to collide with the 3rd person controller to knock it off. I hoped just applying a rigidbody to the object would work, but I guess not.
Thanks!
im struggling with this as well help would be appreciated
Answer by aldonaletto · Jul 28, 2013 at 09:00 PM
If you just want to detect when a rigidbody collides with the CharacterController, use OnCollisionEnter in the character script:
function OnCollisionEnter(col: Collision){
// a rigidbody hit the character - do whatever you want here
}
But if you want the rigidbody to push the CharacterController, you'll need to add specific code to make it move when hit - like the script ImpactReceiver.js (attach to the character):
var mass: float = 3.0; // the lower the mass, the higher the impact
var hitForce: float = 2.5; // impact "force" when hit by rigidbody
private var impact = Vector3.zero; // character momentum
private var character: CharacterController;
function Start(){
character = GetComponent(CharacterController);
}
function AddImpact(force: Vector3){
var dir = force.normalized;
dir.y = 0.5; // add some velocity upwards - it's cooler this way
impact += dir.normalized * force.magnitude / mass;
}
function Update(){
if (impact.magnitude > 0.2){ // if momentum > 0.2...
character.Move(impact * Time.deltaTime); // move character
}
// impact vanishes to zero over time
impact = Vector3.Lerp(impact, Vector3.zero, 5*Time.deltaTime);
}
function OnCollisionEnter(col: Collision){ // collision adds impact
AddImpact(col.relativeVelocity * hitForce);
}
@script RequireComponent(CharacterController)
Looks good, but not sure if I'm doing something wrong somewhere.
I added the script to the 3rd person controller, added a rigidbody to the moving object but nothing seems to happen. I've tried setting the rigidbody to "is kinematic" but didn't change the outcome. I'm not supposed to reference the object with a rigidbody, am I?
This code applies an impact proportional to the velocity of the rigidbody relative to the character - if it's too low, nothing happens, since the resulting impact is lower than 0.2 (line 16 of this code). You may try to increase the field hitForce so that the impact is big enough to be noticed.
I tried adjust the 'hitforce', but no change. All the way upto 1000... $$anonymous$$aybe I'm making a simple error here. Cheers.
i know the answer is extremely late, but you have to use void OnControllerColliderHit (ControllerColliderHit hit) not OnCollisionEnter, which is only for normal rigidbodies
Answer by pugwash · Apr 15, 2015 at 12:12 PM
I had a go with this script and it did not work until I changed the last function as follows. Replace function OnCollisionEnter(col: Collision){ // collision adds impact AddImpact(col.relativeVelocity hitForce); } with the following and make the hitForce about 60. //Tag all the pushing objects as pusher otherwise the character will detect the floor function OnControllerColliderHit(hit : ControllerColliderHit) { if (hit.gameObject.tag=="pusher"){ AddImpact(hit.normal hitForce); } } enjoy