Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 jonik3221 · Jan 09, 2021 at 12:11 AM · freezefixedjointfreezeposition

How to make ball sticky

So I am making a drag and shoot game where you have to make the ball stick to walls to make it to the end of levels and I was wondering if anyone could tell me how to make my Rigidbody2D ball attach itself to 2D Colliders like walls when the ball comes in contact with it.

Comment
Add comment · Show 1
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 Llama_w_2Ls · Jan 09, 2021 at 09:25 AM 0
Share

You could set the rigidbody2D to kinematic or static, when hitting a wall, and resetting it to dynamic when not touching a wall.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tayfundelibalta · Jan 09, 2021 at 12:14 PM

When they are collides,set the ball rigidbody's isKinematic attribute to true.

 void OnCollisionEnter(Collision col)
 {
  if(col.tag=="stickywall")
      myRb.isKinematic=true;
 }

If you want move object with what it sticks to,set stickyObject the ball's parent.

 myObj.transform.parent=stickywall.transform;




Comment
Add comment · Show 4 · 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 jonik3221 · Jan 09, 2021 at 06:40 PM 0
Share

The code makes the ball stick but sometimes when it collides with Collision2Ds it just goes through it

avatar image jonik3221 · Jan 09, 2021 at 06:47 PM 0
Share

This is my current code so far

if (col.tag == currentColor)

{

     }

I just need to add something to make my Rigidbody2D stick to a Collision2D when it comes in contact.

avatar image BrassChuckle · Jan 09, 2021 at 06:51 PM 0
Share

He's close, just change OnCollisionEnter to OnCollisionEnter2D. He's giving you the 3D version, not the 2D.

 void OnCollisionEnter2D(Collision2D col)
  {
   if(col.tag=="stickywall")
       myRb2D.isKinematic=true;               //make sure your rigibbody is 2D as well
  }
avatar image jonik3221 BrassChuckle · Jan 09, 2021 at 07:08 PM 0
Share

Thanks that works but there's another problem my Rigidbody2D keeps rolling when it sticks onto Collision2D

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

113 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

Related Questions

How do you REALLY freeze a Rigid Body (no dragging allowed)? 1 Answer

How do I “freeze” an object after a collision? 3 Answers

Freeze a Rigidbody2D's constraints and move the object manually with translate/Movetowards 1 Answer

Stop player from moving with WASD while using mouse still. 2 Answers

How do I query the RigidbodyConstraints? 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