Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Konomira · Dec 19, 2015 at 09:27 AM · 2d-platformerjumpinglinerenderer

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.

Comment
Add comment · Show 2
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 Veerababu.g · Dec 19, 2015 at 10:03 AM 0
Share

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.

avatar image Konomira Veerababu.g · Dec 19, 2015 at 10:17 AM 0
Share

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".

0 Replies

· Add your reply
  • Sort: 

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

35 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

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

Help making my 2D platformer character jump? 0 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