
grab objet in unity
Publicado por marcos (1 intervención) el 06/06/2025 16:46:57
The following code is used to grab objects in Unity, but when the object is grabbed it stops detecting collisions with everything that doesn't have a rigidbody. Can someone help me to prevent this from happening and so that when the object is in my hand it detects collisions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CogerObjeto : MonoBehaviour
{
public GameObject handPoint;
private GameObject pickedObject = null;
// Update is called once per frame
void Update()
{
if (pickedObject != null)
{
if (Input.GetKey("r"))
{
pickedObject.GetComponent<Rigidbody>().useGravity = true;
pickedObject.GetComponent<Rigidbody>().isKinematic = false;
pickedObject.gameObject.transform.SetParent(null);
pickedObject = null;
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Objeto"))
{
if (Input.GetKey("e") && pickedObject == null)
{
other.GetComponent<Rigidbody>().useGravity = false;
other.GetComponent<Rigidbody>().isKinematic = true;
other.transform.position = handPoint.transform.position;
other.gameObject.transform.SetParent(handPoint.gameObject.transform);
pickedObject = other.gameObject;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CogerObjeto : MonoBehaviour
{
public GameObject handPoint;
private GameObject pickedObject = null;
// Update is called once per frame
void Update()
{
if (pickedObject != null)
{
if (Input.GetKey("r"))
{
pickedObject.GetComponent<Rigidbody>().useGravity = true;
pickedObject.GetComponent<Rigidbody>().isKinematic = false;
pickedObject.gameObject.transform.SetParent(null);
pickedObject = null;
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("Objeto"))
{
if (Input.GetKey("e") && pickedObject == null)
{
other.GetComponent<Rigidbody>().useGravity = false;
other.GetComponent<Rigidbody>().isKinematic = true;
other.transform.position = handPoint.transform.position;
other.gameObject.transform.SetParent(handPoint.gameObject.transform);
pickedObject = other.gameObject;
}
}
}
}
Valora esta pregunta


0