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. :)
↧