• 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 Blixia · Feb 20, 2015 at 05:29 PM · 2drigidbodyfallingsticking

Stop character from falling/sticking to walls

Hello,

I am trying to make my character hang on a wall but I am facing 2 problems.

First problem is how do I know where my character is hanging at? The bottom, left/right side. I am not sure how to get started with this.

My first thought was to put 3 different colliders and give them a name depending on the side there are on. But not sure if this is a good idea performance wise.

Second problem I encounter is how do I stop the character from falling down? I figured this would be the easy part but I am stuck with it as I can't figure out how to make a rigid body stop falling.

I have no idea how to handle the not falling part. First I tried to set gravity to zero on the object but that doesn't work.

The code so far I have although very trail and error:

 public class MovementScript : MonoBehaviour
 {
 
     public float maxSpeed = 5f;
 
     public bool startJump = false;
     public bool isJumping = false;
     public bool isFalling = false;
 
     public float jumpForce = 150f;
 
     //public LayerMask whatIsGround;
 
     public bool hangingOnWall = false;
     public bool jumpOffWall = false;
     public float jumpDirection;
 
     void Start ()
     {
     
     }
 
     void FixedUpdate()
     {
         float move = Input.GetAxis("Horizontal");
 
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
 
         if(Input.GetButton ("Jump") && (!isFalling && !isJumping ) )
         {
             startJump = true;
         }
 
         if(hangingOnWall && move != 0)
         {
             jumpOffWall = true;
             jumpDirection = move;
         }
 
     }
 
     void Update ()
     {
         if(startJump)
         {
             rigidbody2D.AddForce(new Vector2(0,jumpForce));
             startJump = false;
         }
 
         if (rigidbody2D.velocity.y > 0)
         {
             isJumping = true;
             isFalling = false;
         }
         else if(rigidbody2D.velocity.y < 0)
         { 
             isJumping = false;
             isFalling = true;
         }
         else
         {
             isJumping = false;
             isFalling = false;
         }
 
         if(hangingOnWall)
         {
         }
 
         if(jumpOffWall)
         {
             rigidbody2D.AddForce(new Vector2(jumpDirection,jumpForce));
             jumpOffWall = false;
             hangingOnWall = false;
             jumpDirection = 0f;
         }
     }
 
     void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.collider.gameObject.layer == LayerMask.NameToLayer("Ground"))
         {
             Debug.Log("on the ground");
         }
         if (collision.collider.gameObject.layer == LayerMask.NameToLayer("Walls"))
         {
             if(rigidbody2D.velocity.x == 0)
             {
                 Debug.Log (rigidbody2D.velocity);
                 hangingOnWall = true;
             }
         }
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gabriel Saldanha · Feb 20, 2015 at 06:17 PM

Hey there!

1 - About the performance using the colliders: I'm not an expert about that, but I'm pretty sure that 3 colliders is no big deal :) So if you are worried about perfomance, do it!

2 - Handling the 'not falling' part: Why setting the gravity to 0 did not work? It should've. Could you describe what happened? The player kept falling, didn't fall nor jumped off the wall.. Probably there is a logic problem with the flags (isHanging, isFalling, etc.).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

my object not falling 1 Answer

Megaman Movement 0 Answers

How would I make a route that shows where player moves beforehand? 0 Answers

2d sprite falls through floor 0 Answers

2D sprite falls through ground 2 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