- Home /
Cloned gamObject have no gravity on release/detach,Cloned gameObject have no gravity
Hello everyone,
I am testing XR stuffs, and I'm messing around with the grab event. In this test, I want to "clone" a gameObject so it can be grab indeffinetely (and copied)
This is how I do it
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class CloneOnGrab : MonoBehaviour
{
XRGrabInteractable interactable;
Vector3 originPos;
private void Awake()
{
interactable = GetComponent<XRGrabInteractable>();
originPos = gameObject.transform.position;
}
private void OnEnable()
{
interactable.selectEntered.AddListener(OnSelect);
}
private void OnDisable()
{
interactable.selectEntered.RemoveListener(OnSelect);
}
private void OnSelect(SelectEnterEventArgs arg0) => CloneIt();
// HERE : I use the unity Instantiate method to create my new object and give it the position of the current one (right before it get grab)
private void CloneIt()
{
GameObject newclone = Instantiate(gameObject);
newclone.transform.position = originPos;
}
}
I have 2 problems:
The object get cloned when I grab, but it also get cloned when I release and I have NO CLUE why, since I remove the listener during the onDisable method
The "new" GameObject have no gravity when I release it
For this point (my main question) I need to give you more info. The original GameObject is a cube with no gravity. But I have check the ForceGravityOnDetach
variable in the XRGrabInteractable
script attached to it, so when I release it, it falls on the ground. The cloned guys doesn't...
,Hello everyone,
I am testing XR stuffs, and I'm messing around with the grab event. In this test, I want to "clone" a gameObject so it can be grab indeffinetely (and copied)
This is how I do it
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class CloneOnGrab : MonoBehaviour
{
XRGrabInteractable interactable;
Vector3 originPos;
private void Awake()
{
interactable = GetComponent<XRGrabInteractable>();
originPos = gameObject.transform.position;
}
private void OnEnable()
{
interactable.selectEntered.AddListener(OnSelect);
}
private void OnDisable()
{
interactable.selectEntered.RemoveListener(OnSelect);
}
private void OnSelect(SelectEnterEventArgs arg0) => CloneIt();
// HERE : I use the unity Instantiate method to create my new object and give it the position of the current one (right before it get grab)
private void CloneIt()
{
GameObject newclone = Instantiate(gameObject);
newclone.transform.position = originPos;
}
}
I have 2 problems:
The object get cloned when I grab, but it also get cloned when I release and I have NO CLUE why, since I remove the listener during the onDisable method
The "new" GameObject have no gravity when I release it
For this point (my main question) I need to give you more info. The original GameObject is a cube with no gravity. But I have check the ForceGravityOnDetach
variable in the XRGrabInteractable
script attached to it, so when I release it, it falls on the ground. The cloned guys doesn't...