- Home /
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!
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;
$$anonymous$$inematic is normaly used for moving plataforms and sutch
Thank's so much! I never thought to disable the gravity or to make them kinematic! Thanks for the quick answer!
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.
Perfect! This will work great for my sprites that are not rigid-bodies in the project. Thanks for the help!
Your answer
Follow this Question
Related Questions
Make characters go through each other 1 Answer
Character doesen't collide 3 Answers
2D Movement [HELP!] 0 Answers
Character Axes Rotation Problem 3 Answers