I have made a simple 2d character with movement using rigid-body physics, however when it tries to jump along side a wall, the friction causes it to stop half way up the wall, which is unrealistic and makes it difficult to jump in certain places. How can I prevent friction along vertical surfaces?
Here is my code:
public var spawnPoint : Transform;
public var deathPoint : Collider;
public var horizontalSpeed : float = 5.0;
public var verticalSpeed : float = 5.0;
public var acceleration : float = 5.0;
public var deceleration : float = 5.0;
public var jumpStrength : float = 5.0;
public var glideStrength : float = 2.0;
public var stampStrength : float = 5.0;
public var maxJumpSlope : float = 45.0;
@HideInInspector
public var horizontalVelocity : Vector2;
@HideInInspector
public var verticalVelocity : Vector2;
@HideInInspector
public var grounded : boolean = false;
@HideInInspector
public var xVelocity : float;
//move player to spawn point
transform.position = spawnPoint.position;
function FixedUpdate () {
//jump only if player is touching ground
if (Input.GetAxis("Vertical") > 0) {
if (grounded) {
rigidbody.AddForce(Vector2(0,jumpStrength));
}
else {
rigidbody.AddForce(Vector2(0,glideStrength));
}
}
else if (Input.GetAxis("Vertical") < 0) {
rigidbody.AddForce(Vector2(0,-stampStrength));
}
//maintain player vertical velocity at consistent max speed
verticalVelocity = Vector2(0,rigidbody.velocity.y);
if (verticalVelocity.magnitude > verticalSpeed) {
verticalVelocity.Normalize();
verticalVelocity *= verticalSpeed;
rigidbody.velocity.y = verticalVelocity.y;
}
//accelerate player from user input
rigidbody.AddForce(Vector2(Input.GetAxis("Horizontal") * acceleration,0));
//maintain player horizontal velocity at consistent max speed
horizontalVelocity = Vector2(rigidbody.velocity.x, 0);
if (horizontalVelocity.magnitude > horizontalSpeed) {
horizontalVelocity.Normalize();
horizontalVelocity *= horizontalSpeed;
rigidbody.velocity.x = horizontalVelocity.x;
}
//decelerate player if on ground
if (grounded && Input.GetAxis("Horizontal") == 0) {
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x,0,xVelocity,deceleration);
}
}
function OnCollisionStay (collisionInfo : Collision) {
for (var contact : ContactPoint in collisionInfo.contacts) {
//grounded is true if the slope the player is standing on is below a set angle
if (Vector3.Angle(Vector3.up, contact.normal) < maxJumpSlope) {
grounded = true;
}
//respawn player if fallen from edge
if (contact.otherCollider == deathPoint) {
transform.position = spawnPoint.position;
}
}
}
function OnCollisionExit () {
grounded = false;
}