- Home /
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.
You could set the rigidbody2D to kinematic or static, when hitting a wall, and resetting it to dynamic when not touching a wall.
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;
The code makes the ball stick but sometimes when it collides with Collision2Ds it just goes through it
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.
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
}
Thanks that works but there's another problem my Rigidbody2D keeps rolling when it sticks onto Collision2D
Your answer
Follow this Question
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