- Home /
How to prevent friction for character along vertical surfaces?
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;
}
Answer by harschell · Dec 03, 2012 at 11:47 AM
Hi, @adgeofgalaxy
Static Colliders ::
A Static Collider is a GameObject that has a Collider but not a Rigidbody. Static Colliders are used for level geometry which always stays at the same place and never moves around. You can add a Mesh Collider to your already existing graphical meshes (even better use the Import Settings Generate Colliders check box), or you can use one of the other Collider types.
You should never move a Static Collider on a frame by frame basis. Moving Static Colliders will cause an internal recomputation in PhysX that is quite expensive and which will result in a big drop in performance. On top of that the behaviour of waking up other Rigidbodies based on a Static Collider is undefined, and moving Static Colliders will not apply friction to Rigidbodies that touch it. Instead, Colliders that move should always be Kinematic Rigidbodies.
UseFull links::1] Physics
@adgeofgalaxy Hope this info helps you out, Do accept it as Answer & Vote it Up if you find its HelpFull :)
Thanks, harschell!
I made a physics material for the character and set the static friction to 0, the dynamic friction to 0, and the friction combine to $$anonymous$$imum, and it was fixed!
Thanks again, adgeofgalaxy
@adgeofgalaxy you are welcome $$anonymous$$y Friend... The only reason we are here to solve each others problems by sharing our knowledge....
I really don't see how harschell's answer had ANYTHING to do with this question... It may be additional information, but it doesn't address the specific question whatsoever.
The page he linked there is outdated. As of Unity 5 (and updating to PhysX 3), there is no more support for anisotropic friction (i.e. directional friction), and the Physic$$anonymous$$aterial component no longer has that value.
Going on about something that has nothing to do with the question then linking to a page that happens to contain information that at some point in the past may have led to a solution is not a proper answer. Let alone the fact that the information that would have led to a solution on that page is no longer valid, anyway.
UPDATE: $$anonymous$$oving static colliders no longer incurs penalties in Physx. See here
Answer by Max_power1965 · Dec 15, 2015 at 09:54 PM
There is new unity component called platform Effector 2D that will allow you to do that: more info here: http://docs.unity3d.com/Manual/class-PlatformEffector2D.html