- Home /
"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!
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.
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!
A lowercase e would work indeed. Alternatively, you could use getbutton along with editing your input settings.
BCE0019 : 'gameObject' is not a member of 'UnityEngine.RaycastHit'.
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
Can you tell me more about that? it seems like the most logical thing to do...
Logical that changing the parent would be the best way...
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 :)
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
Your answer
Follow this Question
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