- Home /
Other
Problem with collisions of my grab object
Hi! i have a object that can be grabbed, but if this object hit with other objects when i'm grabbing it, it move out of control. How i can make this object, remains in my hands when hit with other objects, thanks :)
public class PickUpObjects : MonoBehaviour
{
public GameObject ObjectToPickUp;
GameObject PickedObject;
public Transform destination;
void Update()
{
if (ObjectToPickUp != null && ObjectToPickUp.GetComponent<PickableObject>().isPickable == true && PickedObject == null)
{
if (Input.GetMouseButtonDown(0))
{
PickedObject = ObjectToPickUp;
PickedObject.GetComponent<PickableObject>().isPickable = false;
PickedObject.transform.SetParent(destination);
PickedObject.transform.position = destination.position;
PickedObject.GetComponent<Rigidbody>().useGravity = false;
}
}
else if (PickedObject != null)
{
if (Input.GetMouseButtonUp(0))
{
PickedObject.GetComponent<PickableObject>().isPickable = true;
PickedObject.transform.SetParent(null);
PickedObject.GetComponent<Rigidbody>().useGravity = true;
PickedObject = null;
}
}
}
}
-----------------------------------------------------------------------------------------------
public class PickableObject : MonoBehaviour
{
private bool isPickable = true;
void OnTriggerEnter(Collider other)
{
if (other.tag =="Destination")
{
other.GetComponentInParent<PickUpObjects>().ObjectToPickUp = this.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Destination")
{
other.GetComponentInParent<PickUpObjects>().ObjectToPickUp = null;
}
}
}
Answer by GilbertoBitt · Sep 11, 2019 at 11:49 AM
why don't you try when been drag put the dragged object to isKinematic = true?
"If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position. Kinematic bodies also affect the motion of other rigidbodies through collisions or joints. Eg. can connect a kinematic rigidbody to a normal rigidbody with a joint and the rigidbody will be constrained with the motion of the kinematic body. Kinematic rigidbodies are also particularly useful for making characters which are normally driven by an animation, but on certain events can be quickly turned into a ragdoll by setting isKinematic to false."
Yes, but i need that my object collide with other objects (walls for example), if i do that, the object not collide with nothing.
you could add on CollisionEnter2D to detect walls and make it non-kinematic just in those cases.
Follow this Question
Related Questions
Box Colider not working properly 1 Answer
Sound volume on different colisions 1 Answer
GUI text like GTA 2 Answers
Duplicated object doesn't collide but original does 1 Answer
Change Colider to Raycast instead? 2 Answers