- Home /
Question by
HammockHead · Dec 16, 2012 at 07:54 AM ·
collisionphysicscontrollerrealistic
How can I have better physics interaction with a FPS Controller
I have tried the Rigid Body Controller and found that my character slides backwards and randomly flies off. I also tried making my own script (relavent parts of script below). However, objects still don't move realistically and my character is also thrown into the air.
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, hit.moveDirection.y, 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.AddRelativeForce ( pushDir * speed * pushPower );
}
Comment
Best Answer
Answer by deltamish · Dec 16, 2012 at 09:35 AM
Hi, Why are using two forces (spped * pushPower).This might add more force to the object to make it fly away and whare you using y axis in pushDir.Dont use y axis in the pushDir this will make the object to fly vertically. Add these few Changes to your script From
var pushPower:2;////i dont know whats your value
var speed:3///i dont know whats your value
function OnControllerColliderHit (hit : ControllerColliderHit){
/// your other code
var pushDir = Vector3 (hit.moveDirection.x, hit.moveDirection.y, 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.AddRelativeForce ( pushDir * speed * pushPower );
}
To
var pushPower:float = 4;//any value
function OnControllerColliderHit (hit : ControllerColliderHit){
////your other code
var pushDir:Vector3 = Vector3(hit.moveDirection.x,0,hit.moveDirection.z);
///push
body.velocity = pushDir * pushPower;
// you can alos use body.AddForce
}