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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RopeScript : MonoBehaviour
{
public Vector3 DestinationHook;
public float HookSpeed = 1f;
public GameObject NodePrefab;
public GameObject lastNode;
public bool HookOnDestination;
public GameObject Target;
public GameObject HookLauncher;
public bool Fired;
public bool NodeCreation;
public bool done = false;
public float Distance = 1f;
public int NumberOfNode;
// Use this for initialization
void Start()
{
lastNode = HookLauncher.gameObject;
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, Target.transform.position, HookSpeed);
if (NodeCreation == true)
{
if (Vector3.Distance(transform.position, lastNode.transform.position) > Distance)
{
CreateNode();
}else if (done == false)
{
done = true;
lastNode.GetComponent<HingeJoint>().connectedBody = HookLauncher.GetComponent<Rigidbody>();
}
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Target")
{
NodeCreation = false;
}
}
public void CreateNode()
{
Vector3 pos2create = HookLauncher.transform.position - lastNode.transform.position;
pos2create.Normalize();
pos2create *= Distance;
pos2create += (Vector3)lastNode.transform.position;
GameObject go = (GameObject)Instantiate(NodePrefab, pos2create, Quaternion.identity);
go.transform.SetParent(transform);
lastNode.GetComponent<HingeJoint>().connectedBody = go.GetComponent<Rigidbody>();
lastNode = go;
}
}