Drawing a "rope" when the player jumps while not grounded
So I'm implementing a ninja rope mechanic in my 2D platformer, which will activate when the user presses "W" while in mid air. The mechanics work well enough, however I have two problems. The first problem is that the rope appears on the character's first jump (jumping while they are grounded). The second is that on the last rope it only flashes visible for a moment before disappearing.
Here's my entire rope code:
#region rope
Vector2 ropePosition = transform.FindChild("rope").GetComponent<Transform>().position;
ropePosition.x = GetComponent<Transform>().position.x + GetComponent<Rigidbody2D>().velocity.x;
ropePosition.y = GetComponent<Transform>().position.y + (height * 3);
if (Physics2D.Raycast(GetComponent<Transform>().position, ropePosition - new Vector2(GetComponent<Transform>().position.x, GetComponent<Transform>().position.y), Vector2.Distance(GetComponent<Transform>().position, ropePosition)))
{
if (Input.GetKey(KeyCode.W) && !grounded && ropesLeft > 0) // If the player: is off the ground, is pressing W, has ropes, is able to rope
{
RaycastHit2D[] ropeHit;
ropeHit = Physics2D.RaycastAll(GetComponent<Transform>().position, ropePosition - new Vector2(GetComponent<Transform>().position.x, GetComponent<Transform>().position.y), Vector2.Distance(GetComponent<Transform>().position, ropePosition));
ropeLine.SetPosition(0, GetComponent<Transform>().position);
if (ropeHit[0])
{
ropeLine.enabled = true;
if (ropeHit[0].collider.tag == "floor" && Input.GetKeyDown(KeyCode.W) && !grounded)
{
ropeLine.SetPosition(1, ropeHit[0].point);
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce((ropePosition - new Vector2(GetComponent<Transform>().position.x, GetComponent<Transform>().position.y)) * 75);
ropesLeft--;
}
}
}
else
{
ropeLine.enabled = false;
}
}
transform.FindChild("rope").GetComponent<Transform>().position = ropePosition;
#endregion
and my OnCollisionEnter/Exit/Stay2D() functions
void OnCollisionStay2D(Collision2D col)
{
// If the floor is below the active character then we are on the ground
if (col.contacts[0].point.y < GetComponent<Transform>().position.y - (height / 2))
{
grounded = true;
}
//if(col.collider.tag == "enemy" && col.gameObject.GetComponent<enemyControls>().alive)
//{
// alive = false;
//}
}
void OnCollisionExit2D(Collision2D col)
{
// If col has the tag "floor" and the floor is below the active character then we are no longer on the ground
if (col.contacts[0].point.y < GetComponent<Transform>().position.y - (height / 2))
{
grounded = false;
ropesLeft = maxRopes;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.contacts[0].point.y < GetComponent<Transform>().position.y - (height / 2))
{
}
}
If you need to see any other code in order to help me, please ask and thank you in advance.
thanks, for so many days am trying to do such type of rope .i could not understand your script .can u tell me how to use your script.
Well right now it doesn't work the best. I use a LineRenderer called ropeLine in the program and what I do is set the initial position at my character and another position that is deter$$anonymous$$ed by the velocity of my character. I raycast to see if it hits a platform and if it does it propels the character in the direction of the rope.
The rope itself is purely aesthetic and the mechanics work similar to a double jump, meaning that the character can't "swing" from the rope. As mentioned in the top part of my question, it does have a few problems but they are merely graphical.
If you wanted to use the above script, you should place the rope region in your void Update()
or wherever you have your movement script. You will also have to declare a LineRenderer ropeLine;
at the beginning of the script as well as int ropesLeft;
and public int maxRopes = (however many ropes you want);
. $$anonymous$$y character also has an invisible child object called "rope" and all my platforms are tagged with "floor".
Your answer
Follow this Question
Related Questions
I can't do jump in my 2D game 1 Answer
How to control how far a character can jump 0 Answers
Keep LineRenderer from changing width in 2D 1 Answer
Jumping on enemies help 0 Answers