• 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 /
avatar image
0
Question by mangozz · Feb 25, 2019 at 06:02 PM · parent

Why does my character not detach from the rope

I have a rope swing and have managed to get the player to attach to the rope using a parent system, but I want it so that if the player presses a button then the character will jump off and detach from the rope. I have done Debugs and it says that the button is not being pressed when on the ground which is good but then as soon as he touches the rope the button is pressed which is not? Thanks.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RopeAttach : MonoBehaviour
 {
     public GameObject player;
     public Vector3 pos;
     public Rigidbody PlayerRB;
     private Collider rope_collider;
     private bool APress = false;
     private bool OnRope = false;
 
 
     void Start()
     {
         
         player = GameObject.Find("player");
         PlayerRB = player.GetComponent<Rigidbody>();
         rope_collider = GetComponent<Collider>();
 
     }
 
     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.Joystick1Button0))
         {
             APress = true;
         }
         Debug.Log(APress);
     }
 
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject == player)
         {
             OnRope = true;
             player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
             PlayerRB.useGravity = false;
             player.transform.parent = transform;
             
 
 
            
         }
     }
 
     void RopeExit()
     {
         if (APress == true && OnRope == true)
         {
 
             rope_collider.enabled = false;
             player.transform.parent = null;
             player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
             player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
             PlayerRB.useGravity = true;
             player.transform.parent = null;
             APress = false;
             OnRope = false;
         }
     }
 
     
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tsaras · Feb 25, 2019 at 06:59 PM

As soon as the Update method catches a button press, it sets APress to true and holds that value from this point onwards. When RopeExit() fires, it will always find APress == true if the button was pressed at anypoint beforehand. I assume this is your problem even though you don't show us where RopeExit() is being called.

You can add an else branch to change APress to false or simply add the check for button press in the if statement inside the RopeExit() function.

          if (Input.GetKeyDown(KeyCode.Joystick1Button0))
          {
              APress = true;
          } else {
               APress = false;
          }

or

  void RopeExit()
  {
      if (Input.GetKeyDown(KeyCode.Joystick1Button0) == true && OnRope == true)
      {
      //rest of code
     //(this actually requires that RopeExit() is called every frame, because GetKeyDown will only be true on the frame where the button was pressed)


Comment
Add comment · Show 6 · Share
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 mangozz · Feb 25, 2019 at 07:20 PM 0
Share

Thanks for the reply, Ive used the first method and the button now seems to be working however the character still doesnt jump off, as it doesnt seem like he is being removed as a child from the rope. Do you have any ideas why this is?

avatar image LilGames mangozz · Feb 25, 2019 at 08:06 PM 0
Share

Is it possible your collision to attach the character TO the rope get's re-triggered?

avatar image mangozz LilGames · Feb 25, 2019 at 08:13 PM 0
Share

It could be possible but I have looked, as my code is not complete I can still walk a bit on the rope and it seems that I can get off the two colliders and then press the button but still doesnt work? alt text

red.jpg (344.2 kB)
avatar image Tsaras mangozz · Feb 25, 2019 at 08:18 PM 0
Share

You need to show us where you are calling the RopeExit() function.

avatar image mangozz Tsaras · Feb 25, 2019 at 08:25 PM 1
Share

Sorry feel stupid, just realized I didnt save the file with the call function it it.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

101 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 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 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

Make a simple tree 1 Answer

Raycast always affects the same object 1 Answer

access grandfather of an object 3 Answers

[C#] How do I instantiate multiple objects at the same time? 1 Answer

Objects falls of the Moving Platform 2 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