- Home /
Question by
unity_O9yFOEld5ESRvw · Jul 31, 2018 at 11:39 PM ·
raycastbeginner3d modelspickup
Cannot pick up an object that is the child of another robject?
Hello, I have this very familiar, very simple code that allows my player to pick up objects and move them around a bit: public GameObject maincam; bool carrying; GameObject carriedObject; public float distance; public float smooth;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (carrying)
{
Carry(carriedObject);
CheckDrop();
}
else
{
Pickup();
}
print(Screen.width);
print(Screen.height);
}
void Carry(GameObject o)
{
o.transform.position = Vector3.Lerp(o.transform.position,maincam.transform.position + maincam.transform.forward * distance, Time.deltaTime * smooth );
}
void Pickup()
{
print("yes we r picking up now");
if (Input.GetKeyDown("space"))
{
print("HELLO!2");
int x = Screen.width /2 ;
int y = Screen.height /2;
Ray ray = maincam.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Pickupable p = hit.collider.GetComponent<Pickupable>();
if (p != null)
{
print("Found something to carrry");
carrying = true;
carriedObject = p.gameObject;
p.gameObject.GetComponent<Rigidbody>().isKinematic = true;
p.gameObject.GetComponent<Collider>().isTrigger = true;
}
}
}
}
void CheckDrop()
{
if (Input.GetKeyDown("space"))
{
Drop();
}
}
void Drop()
{
carrying = false;
carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false;
carriedObject.gameObject.GetComponent<Collider>().isTrigger = false;
carriedObject = null;
}
However, some of the objects that I want to pick up are the children of another object. These are not getting picked up, for some reason. I'm sure there is an obvious way to check for those objects as well, but I haven't found it. Thanks for any and all help!
Comment
Your answer
Follow this Question
Related Questions
Raycasting: Holding an Object 1 Answer
raycast help need 2 Answers
How to SetActive an object with a raycast? 2 Answers
Picking Up AND Hold Onto Items using Raycasting (Bug) 2 Answers