Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kewlhead · Sep 02, 2012 at 05:56 AM · objectdistancekeypressattachpick up

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kewlhead · Sep 02, 2012 at 06:39 AM 0
Share

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!

avatar image grimmdev · Sep 02, 2012 at 08:22 AM 0
Share

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.

avatar image kewlhead · Sep 02, 2012 at 08:51 AM 0
Share

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...

avatar image kewlhead · Sep 02, 2012 at 06:34 PM 0
Share

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;

     }
 }
avatar image grimmdev · Sep 04, 2012 at 10:01 PM 0
Share

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.

avatar image
0

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 = ...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges