The question is answered, right answer was accepted
[Problem] Trying to create a rope for a grappling hook
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;
}
}
Answer by tormentoarmagedoom · Jul 18, 2018 at 12:23 PM
Good day.
Did you tried LineRenderer Component ?
It allows you to draw a line in 3D, it will not move ( at least not by default).
You should investigate and explore if it can be usefull for your propouse!
The other way i can imagine is a little bit more complex and large to do. Have a prefab called "RopeSection", and use them to create a rope by adding a new section at the end of the lastone, controlling its movements by scripts and child/parents configurations. Is more complex, but you can achieve anything by this way!
Good luck!
Thank you for your answer .
I thought about line renderer but I want the rope to move like a real one.
I tried your solution with the hingejoints. Is it not the same?
I've found a way with the Hingejoints but thank you for your advice which gave me ideas for some functions which now works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RopeScript : $$anonymous$$onoBehaviour
{
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 DistanceNodes = 0.1f;
public int NumberOfNode;
// Use this for initialization
void Start()
{
// lastNode = HookLauncher.gameObject;
}
// Update is called once per frame
void Update()
{
if ((NodeCreation == true))
{
CreateNode();
}
if (Fired == true)
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Target.transform.position, HookSpeed);
NodeCreation = true;
}
else
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, HookLauncher.transform.position, HookSpeed);
NodeCreation = false;
}
if (transform.position == Target.transform.position)
{
NodeCreation = false;
}
if ((NodeCreation == true))
{
CreateNode();
}
}
public void CreateNode() {
if (NumberOfNode == 0)
{
GameObject go = (GameObject)Instantiate(NodePrefab, transform.position, Quaternion.identity);
go.transform.SetParent(transform);
HookLauncher.GetComponent<HingeJoint>().connectedBody = go.GetComponent<Rigidbody>();
lastNode = go;
}
else
{
GameObject go = (GameObject)Instantiate(NodePrefab, transform.position, Quaternion.identity);
go.transform.SetParent(transform);
lastNode.GetComponent<HingeJoint>().connectedBody = go.GetComponent<Rigidbody>();
lastNode = go;
}