- Home /
How to have attach and detach Fixed Joints on objects with UI button through Oculus?,How to break fixed joint in a command?
I created a UI Button with the purpose to turn objects into fixed joints whenever is pressed. I want a script where I can press A with the oculus controller over the UI Button and make the cubes join, and I want to press X with the oculus controller over the UI Button to break the joints of the objects. But this is not working for me so far, please any suggestions?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Attach : MonoBehaviour { public GameObject cube; public Text attachText;
private FixedJoint joint;
private float breakForce;
void ClickUIButton()
{
if(attachText.enabled == true) //OVRInput.GetDown(OVRInput.Button.One)
{
if(OVRInput.GetDown(OVRInput.Button.One))
{
join();
}
if(OVRInput.GetDown(OVRInput.Button.Three))
{
Destroy();
}
}
}
private void join()
{
gameObject.AddComponent<FixedJoint>();
gameObject.GetComponent<FixedJoint>().connectedBody = cube.GetComponent<Rigidbody>();
//joint = GetComponent<FixedJoint>();
}
private void Destroy()
{
if (gameObject.GetComponent<FixedJoint>().breakForce == Mathf.Infinity)
{
Destroy(gameObject.GetComponent<FixedJoint>());
}
}
} ,I created a UI button, and when I press A on the oculus controller over the UI button I want to make the cubes have a fixed joint without detaching, and when I press X on the controller over the UI button I want the objects to break joints.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Attach : MonoBehaviour { public GameObject cube; public Text attachText;
private FixedJoint joint;
private float breakForce;
void ClickUIButton()
{
if(attachText.enabled == true) //OVRInput.GetDown(OVRInput.Button.One)
{
if(OVRInput.GetDown(OVRInput.Button.One))
{
join();
}
if(OVRInput.GetDown(OVRInput.Button.Three))
{
Destroy();
}
}
}
private void join()
{
gameObject.AddComponent<FixedJoint>();
gameObject.GetComponent<FixedJoint>().connectedBody = cube.GetComponent<Rigidbody>();
//joint = GetComponent<FixedJoint>();
}
private void Destroy()
{
if (gameObject.GetComponent<FixedJoint>().breakForce == Mathf.Infinity)
{
Destroy(gameObject.GetComponent<FixedJoint>());
}
}
}