- Home /
How to allow player controller to move game objects
The title is basically the question, I want to be able to let the player pick a game object an move it. Not asking any of you to do this for me, but hoping someone has use a tutorial on how to do this before and can point me in the right direction!
Answer by Crystalline · Jun 01, 2015 at 07:05 PM
Create an empty on front of the player ,name it "holdStuff".
Cast a ray from the camera , and make it return the gameObject it hits , reference the hit object as "activeObject" While ray is active and you hold the click button, do :
activeObject.transform. position = holdStuff.transform.position.
Answer by MrBonk71 · Jun 02, 2015 at 08:36 AM
An alternative to the explanation you've already been given is to set the parent of the activeObject transform to be the player's transform + an offset vector (so it's positioned where you want it).
This way, can you simply set it on the mousedown event, then release it in the mouseup event.
Also means you don't have to keep checking stuff every frame to update the position of the object, which is worse for performance than just setting the parent.