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!