- Home /
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
I have done it without the Player script. Jimmy Vegas has a tutorial where he picks up and object with only one script.
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
Answer by john-essy · Sep 21, 2012 at 04:00 PM
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.
Answer by SgtScrambles · Sep 24, 2012 at 02:04 PM
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.
To get a component you would need to reference what you want so say i have a player GameObject in the scene and he is able to move around and do all the things a player would do. But then say i need to be able to access his health this is where i would need to get a component "His Health" so i would do
public PlayerHealth health;
PlayerHealth would be the name of your script you are trying to access. Then in start i would find it
void Start() { health = GameObject("Player").GetComponent(); }
Now i am able to access his health just by dong this
health -= a certain amount.
As for your error you will need to create a Ray or Raycasthit
So you will need to do something like
RaycastHit hit; above the code i provided below this will give you access to there ray class.