- Home /
picking up guns
ive been working on a horror fps game in unity for a while now but i dont know how to pick guns up my game starts off when you are being tortured, but you then somehow kill everyone and escape the room you then find a gun which "you pick up" and escape the building.
so can anyone tell me how i would do this?
Answer by JesusChristChangedMe · Dec 05, 2010 at 08:39 PM
there is a way to do this. you will have to use a parenting technique. makeing the gun a child of the player. this will take work but here is how to do it. First you will have to make a sphere. gameObject-> create other-> sphere. after you have one put it exacly where you want the center of your gun to be spawned at. then drag it in your hierarchy onto your player. thus making it a child of the player. then uncheck the mesh renderer on the sphere and remove the collider component. make sure the player is tagged "Player". make sure that in the script below that the sphere you created goes in the SpawnTo place and that your gun goes in the Gun place. there should be a place to drag it onto after you have attached this script to your gun it should give you
- SpawnTo-(Transform)
- Gun-(Transform)
now make this script.
var SpawnTo:Transform;
var Gun : Transform;
function OnCollisionEnter(hit:Collider)
{
if(hit.gameObject.tag == "Player")
{
Gun.parent = SpawnTo;
Gun.transform.position = SpawnTo.transform.position;
Gun.transform.rotation = SpawnTo.transform.rotation;
}
}
Hope this Helps!
when i copy the script which you have given me an error comes up in saying "parent is not a member of unityengine.gameobject"
what does this mean and how can i fix it?
another error that might come up is about the transform.rotaion, just make that word rotation.
a syntatical way to fix this is chnage the declaration of GameObject in the vars to a Transform. ie: var SpawnTo : Transform;
well i kinda looked at some of my other scripts and edited this one... let me make sure about that parent error. but try this and see what happens.
Your answer
Follow this Question
Related Questions
picking up weapons script? 3 Answers
Weapon pick up 3 Answers
Weapon Prefab 0 Answers
Avart Fate with Gun Movement 0 Answers
Problem With Weapon Firing Mechanism 1 Answer