Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by adgeofgalaxy · Dec 03, 2012 at 08:55 AM · physicsrigidbodycharacter2d-platformerfriction

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

2] Physics Material

@adgeofgalaxy Hope this info helps you out, Do accept it as Answer & Vote it Up if you find its HelpFull :)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adgeofgalaxy · Dec 04, 2012 at 12:11 AM 1
Share

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

avatar image harschell · Dec 05, 2012 at 03:55 AM 0
Share

@adgeofgalaxy you are welcome $$anonymous$$y Friend... The only reason we are here to solve each others problems by sharing our knowledge....

avatar image DESTRUKTORR · Jun 22, 2015 at 12:33 PM 0
Share

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.

avatar image _DkC · Jul 16, 2015 at 11:55 PM 0
Share

The links seem to be the answer to the question.

avatar image DESTRUKTORR _DkC · Sep 27, 2015 at 03:17 PM 2
Share

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.

avatar image Ultroman · Apr 10, 2018 at 02:50 PM 0
Share

UPDATE: $$anonymous$$oving static colliders no longer incurs penalties in Physx. See here

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Friction in unity? 2 Answers

Make rigidbody walk like character controller 6 Answers

How do I make a rigidbody move on command (ie A Player Character)? 1 Answer

RigidBody.AddForce causes capsule collider to trip on a surface with friction 2 Answers

jet pack physics with character controller 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges