- Home /
Holding your gameobjects
hello guys,
i'm trying to make a game where you are able to spawn creatures, when you press a button a prefab will be shown in front of the player, and it should move and rotate with the players point of view.
i'm new to unity and coding in general can someone give me a pointers to how to go about this? i can successfully instatiate the gameobject but i have no idea how to make the gameobject move as my player is moving. And stop moving when the player presses "Fire1"
thanks in advance, any help is greatly appreciated!
Hi, How about if a key is pressed then you run code:
var target : GameObject; function Update () { if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.)){ transform.position = target.transform.position; transform.rotation = target.transform.rotation;
}}
Drag the gameobject into the target slot at the editor view when script is added to playerobject. This would match the position of the target.
Untested but hope it helps :)
Answer by MrSteve1 · Apr 07, 2014 at 07:47 PM
This one should be fairly straight forward,
Store the camera in a Transform variable. In the Start() function, get the camera game object with gameObect.FindWithTag("MainCamera").transform;
an then attach the creature object to the camera using transfom.parent = camObject and then match its transform, then just at roughly 10 units to he Z Axis.
Then as soon as Fire1 is click, set parent to null.
Thank you for the answer!
i just had the time to check it now but as soon as i store the camera in a transform variable (i am using C#) i get the error "cannot implicitly convert type UnityEngine.Transform to UnityEngine.GameObject" is it just me that is writing the code wrong or is this a common problem?
oh! i just wrote the code wrong i figured out what to do thank you very much for the help!
A quick question again.
can i only run the if (input.GetButtonDown("fire1")) statement in the void Update? because it doesnt register when i use the Fire1.
yes correct only in update as it need to know every frrame if being used
Answer by exvalid · Nov 12, 2017 at 10:27 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