- Home /
Physics AddForce reduced when 3 objects are colliding
I am Going through ETeeskiTutorials fps and I am just finnishing up the Rigid body character controller movement script and I have run into a problem. When my character is running around everything seems to be happening as it should, but when the character is touching 2 seperate surfaces (like the floor and a wall, or the seam where two diffrently angled floors are touching) I notice a dramatic reduction in the addition of force to the character. Most noticeably when jumping. Here are the Scripts I am using.
This one is for Camera look:
var lookSensitivity : float = 5;
@HideInInspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var lookSmoothDamp : float = 0.045;
function Update () {
yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
and this one is for movement and jumping:
var walkAcceleration : float = 45;
var runDeceleration : float = 1;
var walkDecelerationVolX : float;
var walkDecelerationVolZ : float;
var walkDeceleration : float = 0.5;
var cameraObject : GameObject;
var maxWalkSpeed : float = 3;
var horizontalMovement : Vector2;
var jumpVelocity : float = 300;
var grounded : boolean = false;
var maxSlope : float = 60;
function Update ()
{
//faces the rigidbody with the camera//
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
//gets current speed as a Vector2//
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
//sets a percentage that is inversely proportional to the speed over maxWalkSpeed//
if(horizontalMovement.magnitude > maxWalkSpeed)
{
runDeceleration = maxWalkSpeed / horizontalMovement.magnitude;
}
//Keeps runDecceleration from getting stuck at a low percentage in the event of sudden decceleration//
else
{
runDeceleration = 1;
}
walkDeceleration = horizontalMovement.magnitude / 25 + 0.1;
//dampend character slide//
if(grounded)
{
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDecelerationVolX, walkDeceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDecelerationVolZ, walkDeceleration);
}
//wasd movement//
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * runDeceleration, 0, Input.GetAxis("Vertical") * walkAcceleration * runDeceleration);
//Jumping//
if(Input.GetButtonDown("Jump") && grounded)
{
rigidbody.AddForce(0, jumpVelocity, 0);
}
}
//Jumping Prerequisits//
function OnCollisionStay(collision : Collision)
{
for(var contact : ContactPoint in collision.contacts)
{
if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
{
grounded = true;
}
}
}
function OnCollisionExit()
{
grounded = false;
}
I am not sure if it is just a quark with the physics engine adding the friction in a funny way when dealing with this situation or if I am approaching the code in a weird way. I have kind of done a bit of modification of it from the original tutorial. Any ways any insight into this anomaly would be greatly appreciated, thanks. :)
Answer by TheLinkingTinker · Apr 02, 2014 at 07:32 AM
OK, after a little research I found that some of the other posts here where dealing with the same problem. With a bit of experimentation I found that rigid bodies behave strangely when in contact with none rigid bodies. It just seems to be a quark in the physics engine. The answer I found is that if you are going to have a rigid body character controller then everything that character comes in contact with must also be a rigid body; the ground, the walls, the other characters. don't forget to set any rigid bodies that intersect to kinematic or some other strange effects will occur. Now my ground and walls are rigid bodies and have kinematic checked and all the freeze position and freeze rotation boxes checked. The problem is now gone... although I can't help but wonder if having every object as a rigid body may be a bit resource intensive.