Simple Object Pick-Up

I am making a game where your only goal is to cause havoc. Right now I have a script where I am able to pick up and move around objects. The only problem is that I pick up all the objects instead of just one.

`var other : Rigidbody;

function Update () {
if(Input.GetButtonDown(“Fire1”)) {
other.transform.parent = transform;

}else if(Input.GetButtonUp("Fire1")) {
    other.transform.parent = null;
}

}
`

is this code on all of the objects? what is other in this context and how/when is it set? need more information to fully understand the problem here.

–

You have to make 2 scripts, 1 for the object you want to pick up and 1 for picking up. Ex : Object script -> transform script If player presses fire moving becomes avabile. Player script -> Pickup script If you press fire moving object with tag becomes avabile

–

ok. there are some ways of doing this. it all depends on how ur game works. are u picking the objects if u get close or if u targe them with the mouse? how?. this would greatly change the script

–

2 Answers

2

If you are picking up objects i would use a RayCast Then within your update method i would do something like

if (Physics.Raycast (transform.position, Vector3.forward, hit, 100.0))`
    {
        if(hit.collider.gameObject.tag == "PickUpableObject")
        {
		hit.transform.parent = transform;
	    }
	else
	{
		hit.transform.parent = null;
	}
}

This way only what ever the ray hit will be picked up.

The code is on the Main Camera. I tried using a RayCast but I kept getting errors like “Assets/Script/PickUpObject.js(5,25): BCE0023: No appropriate version of ‘UnityEngine.Physics.Raycast’ for the argument list ‘(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Rigidbody, float)’ was found.” I havent tried a GetComponent and I’m not sure how I would exactly do that.

Hey, that's a COMMENT, not an ANSWER. ideally simply click EDIT under your own question and add the new information.

–