- Home /
Connect hingejoint with collision object
I'm making a 2d game where you can connect objects by a rope, but i have a problem with the rope that needs to attach two object to each other. I created a rope with two hinge joints, one left that needs to connect one object and one right that needs to connect to the other object that collides. How can I set the connectbody of the hingejoint with the rigidbody of the collided object?
this is my code for now. It's not complete, I want to set the rigidbody of the collision objects to krat1 and krat2.
using UnityEngine;
using System.Collections;
public class ropeScript : MonoBehaviour {
private HingeJoint2D joint1;
private HingeJoint2D joint2;
private Rigidbody2D krat1;
private Rigidbody2D krat2;
private bool hingeTrue;
// Use this for initialization
void Start () {
hingeTrue = true;
print(hingeTrue);
}
void OnCollisionEnter (Collision col)
{
}
// Update is called once per frame
void Update () {
hinge();
}
public void hinge () {
if(Time.timeScale==1 & hingeTrue) {
print("it's true");
joint1 = gameObject.AddComponent<HingeJoint2D>();
joint2 = gameObject.AddComponent<HingeJoint2D>();
krat1 = krat1.GetComponent<Rigidbody2D>();
krat2 = krat2.GetComponent<Rigidbody2D>();
joint1.connectedAnchor = new Vector2(3.7f, 0 );
joint2.connectedAnchor = new Vector2(-3.7f, 0);
joint1.connectedBody = krat1;
joint2.connectedBody = krat2;
hingeTrue = false;
}
}
}
Your answer
Follow this Question
Related Questions
How to set the "Connected Body" of a HingeJoint 2D using Script? 1 Answer
Problem assigning hj.connectedBody 0 Answers
Creating a simple swinging vine/rope/line 0 Answers
how to set the motor speed of hingjoint2D via script ? 0 Answers
Unsing a swinging object, attached to a hinge joint as swinging platform? 0 Answers