- Home /
Picking Up Objects Within Range
I'm in the process of making a very interesting and experimental sandbox, RTS, puzzle, and adventure game all in one, and I'm trying to get one of the key components to work, which is picking up objects. I have tried using raycasts with the mouse, but seeing as it's a 3rd-person game, it doesn't quite function how I'd like it to. What I want to accomplish is as you get close enough to any object able to be picked up (assuming tags here) and when the user presses a key, the object gets carried/dragged along with the character until the key is pressed again. All the key presses and such aren't a problem for me, it's just the actual attaching of the object to the character, and doing it within a certain distance.
Thanks so much!
Answer by grimmdev · Sep 02, 2012 at 06:35 AM
I think a very simple idea for someone who is still learning, would be that you should use collision detection and on enter of an objects collision(boundry box or invisible collider object) and on all objects within collision are entered it picks them up, and the range can be set by the size of the object thats detecting the collision, as so.
http://forum.unity3d.com/threads/33091-SOLVED-Collision-detection-without-physics
possibly a invisible plane/sphere with a size and collision detection without physics should do ok.
I've got the collision detection down, I can destroy objects on collision and all that good stuff, just not sure how to do the actual "grab", whether it be parenting or transfor$$anonymous$$g, I'm not sure how to do that. Thanks though!
Sorry didn't see your comment.You need to explain more, but @Fattie is correct on this, by raycasting and using collision of the raycast. You can then set the object your targeting as the Child of the player, therefore when the player moves the objects move and if you actually mean that they should be picked up on the Y axis you can use Translate.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html
something simple like on Collision translate the target object (0,1f,0) or by what I believe your saying you can translate the object to a position infront of the player and then attach it to the player as a Child like @Fattie mentioned.
I got it figured out by playing around with both suggestions, now I just need to figure out how to break the parent, and drop the item...
So I've got it to where I can pick only one thing up at a time, but when I set the parent to null, nothing happens... Here's my code:
function OnTriggerEnter (col : Collider) {
target = GameObject.FindWithTag("grabbable").transform;
if(col.tag == "grabbable" && FullOrNot.full < 1){
transform.parent = target.transform;
transform.localPosition = Vector3(0, 0, 0);
FullOrNot.full = 1;
} else if(Input.Get$$anonymous$$eyDown ("space") && FullOrNot.full == 1){
transform.parent = null;
FullOrNot.full = 0;
}
}
this might be simple, your using it in the collider, ins$$anonymous$$d, use it in an update method and just check for collision then send the data/variable to the update method, I believe unity see's the if input get space on collision and only works when you collide into an object and hold space.
Answer by manyokibenc · Oct 24, 2014 at 06:33 PM
You must use "raycast", it's the best way! I would like to make a game like that, and I had the same problem, and I find the solution! Here it is:
Vector3 fwd = Camera.main.transform.TransformDirection(Vector3.forward); RaycastHit hit ; if (Physics.Raycast (Camera.main.transform.position, fwd, out hit, 1000)){ //The object in front of you the "hit" var. //You can do something with the object, like this: hit.transform.position = ...
Your answer

Follow this Question
Related Questions
Getting vectors on an object every so many units 0 Answers
object distance 1 Answer
Calculate distance between AR object and camera 1 Answer
Attaching a Camera to the Player 3 Answers
How to do something when an object is in the player sights. 2 Answers