Search Unity

[Problem] Trying creating rope of a grappling hook

Discussion in 'Physics' started by alien30, Jul 17, 2018.

  1. alien30

    alien30

    Joined:
    Oct 16, 2017
    Posts:
    1
    Hello everyone

    Sorry if this is not the good place but I never used this forum before.

    I'm trying to create a grappling hook for a game. I Have a Hook, HookLauncher and Target. The Hook travel from the hookLauncher to the target by clicking on Mouse button Left and reverse without a problem.

    But When I try to creat connected nodes during the hook's movements, it never works. I tried different methods but none of them worked. There is a message telling that a joint can't connect a body to itself.



    Do you have any advises please?

    Thank you for your help, The code inside my hook is here.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RopeScript : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public Vector3 DestinationHook;
    10.  
    11.  
    12.     public float HookSpeed = 1f;
    13.  
    14.  
    15.  
    16.     public GameObject NodePrefab;
    17.  
    18.     public GameObject lastNode;
    19.  
    20.  
    21.     public bool HookOnDestination;
    22.  
    23.  
    24.     public GameObject Target;
    25.  
    26.     public GameObject HookLauncher;
    27.  
    28.  
    29.  
    30.     public bool Fired;
    31.  
    32.     public bool NodeCreation;
    33.  
    34.     public bool done = false;
    35.  
    36.  
    37.     public float Distance = 1f;
    38.  
    39.     public int NumberOfNode;
    40.  
    41.  
    42.  
    43.     // Use this for initialization
    44.     void Start()
    45.     {
    46.  
    47.         lastNode = HookLauncher.gameObject;
    48.  
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update()
    53.     {
    54.  
    55.         transform.position = Vector3.MoveTowards(transform.position, Target.transform.position, HookSpeed);
    56.  
    57.  
    58.         if (NodeCreation == true)
    59.         {
    60.  
    61.             if (Vector3.Distance(transform.position, lastNode.transform.position) > Distance)
    62.             {
    63.  
    64.                 CreateNode();
    65.  
    66.             }else if (done == false)
    67.             {
    68.                 done = true;
    69.  
    70.                 lastNode.GetComponent<HingeJoint>().connectedBody = HookLauncher.GetComponent<Rigidbody>();
    71.             }
    72.  
    73.         }
    74.  
    75.     }
    76.  
    77.  
    78.     public void OnTriggerEnter(Collider other)
    79.     {
    80.         if (other.tag == "Target")
    81.         {
    82.             NodeCreation = false;
    83.         }
    84.     }
    85.  
    86.     public void CreateNode()
    87.     {
    88.  
    89.         Vector3 pos2create = HookLauncher.transform.position - lastNode.transform.position;
    90.         pos2create.Normalize();
    91.         pos2create *= Distance;
    92.         pos2create += (Vector3)lastNode.transform.position;
    93.  
    94.         GameObject go = (GameObject)Instantiate(NodePrefab, pos2create, Quaternion.identity);
    95.  
    96.         go.transform.SetParent(transform);
    97.  
    98.         lastNode.GetComponent<HingeJoint>().connectedBody = go.GetComponent<Rigidbody>();
    99.  
    100.         lastNode = go;
    101.  
    102.     }
    103.  
    104. }