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 DubstepDragon · Feb 18, 2013 at 09:04 PM · objectgamemousehold

"E" to grab an object...

How would it be possible to create a script that will hold an item, keeping it a fair distance from the player, and moving it around as the mouse moves? Can someone help me?

This is not a request for labor, merely me seeking guidance.

Thanks in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Professor Snake · Feb 18, 2013 at 09:58 PM

Slayer29179's way would be the more straightforward way to do it indeed. I suggest taking a look at Physics.Raycast to determine the object you want to pick up, and then use the raycastHit data to see if it can be picked up. If so, change its parent to be the camera and make it get dropped once the player lets go of E. Here is an example script that should be attached to the camera:

 var hit:RaycastHit;
 var pickedUpObject:GameObject;
 
 if(Input.GetKey("e")){
 if(Physics.Raycast(transform.position,transform.forward,hit,100)){//the order of the parameters might be wrong.
 if(hit.gameObject.tag=="DynamicObject"){ //i used a tag to see whether the object cn be picked up, you can use another method that may suit you better
 pickedUpObject=hit.collider.gameObject; //we use this to determine whether an object is picked up by the player. If it's not null, then the player is doing so.
 hit.gameObject.transform.parent=transform; //attach the object to the camera so it moves along with it.
 hit.gameObject.transform.position=transform.position-transform.forward; //might need changing as it's untested.
 }
 }
 }
 else if(pickedUpObject!=null){ //if player is not holding E but was picking up an object last frame
 pickedUpObject.transform.parent=null; //drop the object
 pickedUpObject=null;  //and nullify the object pointer
 }


However, the method to carry it with the camera may lead to some unwanted results such as it clipping through walls. You could either use an additional linecast to determine its position or you could poke around the DragRigidbody.js script and use its method of carrying for a more elegant solution.

Comment
Add comment · Show 12 · 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 DubstepDragon · Feb 19, 2013 at 09:32 AM 0
Share

You're script seems logically possible, except that I am now getting this error: "UnityException: Input $$anonymous$$ey named: E is unknown". I do not know for myself what in particular is causing it, but it's driving me crazy...

Thanks for the good work!

avatar image slayer29179 · Feb 19, 2013 at 09:45 AM 0
Share

Put it as a lower case e! :)

avatar image Professor Snake · Feb 19, 2013 at 11:20 AM 0
Share

A lowercase e would work indeed. Alternatively, you could use getbutton along with editing your input settings.

avatar image Jeredriq · Sep 26, 2013 at 01:28 AM 0
Share

BCE0019 : 'gameObject' is not a member of 'UnityEngine.RaycastHit'.

avatar image Professor Snake · Sep 26, 2013 at 08:49 AM 0
Share

hit.collider.gameObject

Show more comments
avatar image
2

Answer by slayer29179 · Feb 18, 2013 at 09:46 PM

This could be done by changing the parent of the object you get to the camera. Which is done by object.transform.parent = objectName;

For the collection you could either have a trigger or a distance detection :)

Hope this helps :D

Comment
Add comment · Show 3 · 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 DubstepDragon · Feb 19, 2013 at 07:50 PM 0
Share

Can you tell me more about that? it seems like the most logical thing to do...

avatar image DubstepDragon · Feb 19, 2013 at 07:50 PM 0
Share

Logical that changing the parent would be the best way...

avatar image slayer29179 · Feb 19, 2013 at 07:55 PM 0
Share

Something along the lines of when in distance of an object if you press a key, it will change the parent of the object to the center view of the camera to give the illusion of picking it up :)

avatar image
0

Answer by exvalid · Oct 20, 2017 at 07:51 PM

Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

ObjectGrabIdAndLock, ObjectReplyIdAndMove

ObjectGrabIdAndLock goes on Main player,.. ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

cheers Ryan kappeslink text Exvalid@gmail.comlink text


objectgrabscripts.zip (11.7 kB)
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

13 People are following this question.

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

Related Questions

Slicing Objects 1 Answer

pause game 2 Answers

How do I make an object look at a point it is moving toward? 1 Answer

A node in a childnode? 1 Answer

Destroy GameObject and play sound 1 Answer


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