Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved Spring joint wont destroy itself on StopGrapple method.

Discussion in 'Editor & General Support' started by Gristle18, Jul 31, 2021.

  1. Gristle18

    Gristle18

    Joined:
    Jul 23, 2021
    Posts:
    8
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GrappleGun : MonoBehaviour
    6. {
    7.     private LineRenderer linerenderer;
    8.     private Vector3 grapplePoint;
    9.     public LayerMask grappleable;
    10.     public GameObject randomShit;
    11.     public Transform gunTip;
    12.     [SerializeField] float maxDistance;
    13.     private SpringJoint joint;
    14.     public GameObject XRRig;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.  
    19.         linerenderer = GetComponent<LineRenderer>();
    20.     }
    21.  
    22.     void LateUpdate()
    23.     {
    24.         DrawRope();
    25.     }
    26.  
    27.     public void StartGrapple()
    28.     {
    29.         Debug.Log("Grapple Started");
    30.         RaycastHit hit;
    31.         if (Physics.Raycast(gunTip.position,gunTip.forward,out hit,maxDistance,grappleable))
    32.         {
    33.             Debug.Log("raycast hit");
    34.             Debug.Log(hit);
    35.             grapplePoint = hit.point;
    36.             joint = XRRig.AddComponent<SpringJoint>();
    37.             joint.autoConfigureConnectedAnchor = false;
    38.             joint.connectedAnchor = grapplePoint;
    39.  
    40.             float distanceFromPoint = Vector3.Distance(gunTip.position,grapplePoint);
    41.  
    42.             //distace grapple will try to keep from grapple point
    43.             joint.maxDistance = distanceFromPoint * .8f;
    44.             joint.minDistance = distanceFromPoint * .25f;
    45.  
    46.             //Feel of grapple
    47.             joint.spring = 45f;
    48.             joint.damper = 7f;
    49.             joint.massScale = 4.5f;
    50.  
    51.            
    52.         }
    53.  
    54.         linerenderer.positionCount = 2;
    55.     }
    56.  
    57.     public void StopGrapple()
    58.     {
    59.         Debug.Log("grappleStoped");
    60.         linerenderer.positionCount = 0;
    61.         Destroy(joint);
    62.     }
    63.  
    64.     void DrawRope()
    65.     {
    66.         //dont draw if not grappled
    67.         if (!joint) return;
    68.  
    69.         linerenderer.SetPosition(0, gunTip.position);
    70.         linerenderer.SetPosition(1, grapplePoint);
    71.    }
    72. }

    So basically I have a grappling system in my game and so far the SpringJoint is made, the LineRenderer is drawn and I know that the StopGrapple method is working. I'm really struggling to try to fix this. If anyone could please help that would be great thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Do you see the Debug.Log()? If not, find out why not.

    If you do, then replace it with a Debug.Break() to pause the editor, then go ransack your scene and find that spring joint. Is it still present?
     
  3. Gristle18

    Gristle18

    Joined:
    Jul 23, 2021
    Posts:
    8
    Well, I am using the new event-based input system and I've noticed that it calls the start grapple method twice but the stop grapple method once. I'm thinking that maybe I'm making two spring joints and then destroying one or would destroy(joint) destroy both of the joints?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Destroy() would only destroy what you tell it (and any children).
     
  5. Gristle18

    Gristle18

    Joined:
    Jul 23, 2021
    Posts:
    8
    I actually just checked that but only one joint is being made it's just not being destroyed. Is there a different way to write the script that might destroy it?
     
  6. Gristle18

    Gristle18

    Joined:
    Jul 23, 2021
    Posts:
    8
    Okay Okay I was very wrong actually two spring joints were being made but only one was being destroyed I fixed it by adding a bool grappled and every time raycast hit it was set to true and every time stop grapple method was run it was set to false so it cant make another spring joint before the stop grapple meathod is run. Thank You for all your help!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GrappleGun : MonoBehaviour
    6. {
    7.     private LineRenderer linerenderer;
    8.     private Vector3 grapplePoint;
    9.     public LayerMask grappleable;
    10.     public GameObject randomShit;
    11.     public Transform gunTip;
    12.     [SerializeField] float maxDistance;
    13.     private SpringJoint joint;
    14.     public GameObject XRRig;
    15.     private bool grappled;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.  
    20.         linerenderer = GetComponent<LineRenderer>();
    21.     }
    22.  
    23.     void LateUpdate()
    24.     {
    25.         DrawRope();
    26.     }
    27.  
    28.     public void StartGrapple()
    29.     {
    30.         Debug.Log("Grapple Started");
    31.         RaycastHit hit;
    32.         if ((Physics.Raycast(gunTip.position,gunTip.forward,out hit,maxDistance,grappleable)) && (!grappled))
    33.         {
    34.             grappled = true;
    35.             Debug.Log("raycast hit");
    36.             Debug.Log(hit);
    37.             grapplePoint = hit.point;
    38.             joint = XRRig.AddComponent<SpringJoint>();
    39.             Debug.Log(joint);
    40.             joint.autoConfigureConnectedAnchor = false;
    41.             joint.connectedAnchor = grapplePoint;
    42.  
    43.             float distanceFromPoint = Vector3.Distance(gunTip.position,grapplePoint);
    44.  
    45.             //distace grapple will try to keep from grapple point
    46.             joint.maxDistance = distanceFromPoint * .8f;
    47.             joint.minDistance = distanceFromPoint * .25f;
    48.  
    49.             //Feel of grapple
    50.             joint.spring = 45f;
    51.             joint.damper = 7f;
    52.             joint.massScale = 4.5f;
    53.  
    54.             linerenderer.positionCount = 2;
    55.         }
    56.  
    57.        
    58.     }
    59.  
    60.     public void StopGrapple()
    61.     {
    62.         linerenderer.positionCount = 0;
    63.         Destroy(joint);
    64.         grappled = false;
    65.     }
    66.  
    67.     void DrawRope()
    68.     {
    69.         //dont draw if not grappled
    70.         if (!joint) return;
    71.  
    72.         linerenderer.SetPosition(0, gunTip.position);
    73.         linerenderer.SetPosition(1, grapplePoint);
    74.    }
    75. }
     
    Kurt-Dekker likes this.
  7. alekz1k

    alekz1k

    Joined:
    Dec 14, 2014
    Posts:
    16