Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by E_101 · Jan 21, 2018 at 01:09 AM · collisionmovementcharacter2d-physicsphysics material

How to Make Characters Stick to Walls? 2D (c#)

I am working on a project that requires the player to be able to not only stick to a wall but also be able to jump off the wall and stick to another wall. Every time I try to mess wit the RigidBody2D variables it does not seem to make any difference, the character simple slides off. I already have the character movement working properly but figuring out how to make the character be able to stick to the walls is difficult. I also have a friction material on the wall already. Is it possible to allow the character to not be a rigid body? Any help is appreciated! Thanks in advance!

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

Answer by lassade · Jan 21, 2018 at 05:07 AM

Yes you can try two things make the rigidbody kinematic (best bet in my option) or turn off the gravisty by doing myRigidbody2D.gravityScale = 0;.

Kinematic means in simple terms that the rigidbody will ignore external forces, but still can move and interact with other rigidbodies;

Comment
Add comment · Show 2 · 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 lassade · Jan 21, 2018 at 05:08 AM 0
Share

$$anonymous$$inematic is normaly used for moving plataforms and sutch

avatar image E_101 · Jan 21, 2018 at 09:32 AM 0
Share

Thank's so much! I never thought to disable the gravity or to make them kinematic! Thanks for the quick answer!

avatar image
1

Answer by Calesurf · Jan 21, 2018 at 07:26 AM

A while ago I made a game very similar to this. It was basically a square that would jump around a scene and stick to walls. The way I approached it was to freeze the X and Y positions as well as the Z rotation whenever OnCollisionEnter() was triggered.

Here is the code I used for my game.

     void OnCollisionEnter2D(Collision2D collison){
         if(collison.transform.tag != "Player"){
             if (!Input.GetMouseButtonDown (0)) {
                 body.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
                 //print ("Col");
                 canJump = true;
                 //print ("Enter");
             }
         } 
     }
     void OnCollisionExit2D(Collision2D collision){
         if(collision.transform.tag != "Player"){
             body.constraints = RigidbodyConstraints2D.None;
             canJump = false;
             //print ("Exit");
         }
     }
     void OnCollisionStay2D(Collision2D collision){
         if(collision.transform.tag != "Player"){
             //body.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
             canJump = true;
             //rigidbody2D.v = new Vector2 (0, 0);
             //print ("Stay");
         }
     }

Hope this helps.

Comment
Add comment · Show 1 · 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 E_101 · Jan 21, 2018 at 09:32 AM 0
Share

Perfect! This will work great for my sprites that are not rigid-bodies in the project. Thanks for the help!

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

173 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Make characters go through each other 1 Answer

Character doesen't collide 3 Answers

2D Movement [HELP!] 0 Answers

Why doesn't my collision detection script work when using OnTriggerEnter, however when using OnCollisionEnter it doesn't work at all... Would really like to know.. Thank you in advance 2 Answers

Character Axes Rotation Problem 3 Answers


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