Trying to spawn a physics prop above the players head
1 replies, posted
Im trying to get the players location and spawn a babydoll above and have it fall on the player.
Dont worry this is not for a gamemode; I'm trying to learn how to use ent's and player locations.
Any feedback is appreciated, thank you.
[code]function spawn_prop()
local prop=ents.Create("prop_physics")
prop:SetModel("models/props_c17/doll01.mdl")
local function getplyPos(ply)
player:GetLocation
return (plyloc)
prop:SetPos(plyloc)
end
end
hook.Add( "InitPostEntity", "hookName", function() timer.Create( "createAllTheBabies", .5, 0, function() spawn_prop() end ) end ) [/code]
Ok, first of all, instead of making your hook function be
[CODE]function() function_call() end[/CODE]
just do
[CODE]hook.Add("blah", "blah", spawn_prop)[/CODE]
I'm going to just format your code for you here too
[CODE]
function spawn_prop()
local prop=ents.Create("prop_physics")
prop:SetModel("models/props_c17/doll01.mdl")
local function getplyPos(ply)
player:GetLocation
return (plyloc)
prop:SetPos(plyloc)
end
end
hook.Add( "InitPostEntity", "hookName", function() timer.Create( "createAllTheBabies", .5, 0, function() spawn_prop() end )
end )
[/CODE]
Ok, so you have an extra end ) at the end there that is not needed. Now for fixing the actual code, there is a function on the player metatable (a metatable is like a recipe for making a player; every player has that set of functions with it) called GetPos and EyePos (for getting the feet location and eye location respectively). In your case, you want it relative to the head (i.e. if they crouch, it should be lower), so use EyePos() and add maybe like 50 to the z to lift it above their eyes.
So for your spawn_prop function I'd do this:
[CODE]
local function spawn_prop() -- better to use a local function
local prop=ents.Create("prop_physics") -- Ok, this looks good, make the physics prop
prop:SetModel("models/props_c17/doll01.mdl") -- And set its model
-- not sure how you determine which player you're spawning this baby for, but here's an example:
prop:SetPos( player.GetAll()[1]:EyePos() + Vector(0,0,50) ) -- this will spawn the prop 50 source units above the players eye's (i.e. above their head). Vector is a function which takes in (x,y,z) and creates a Vector. z is up and down so adding Vector(0,0,50) will raise z by 50, raising the position upwards 50 units.
end
[/CODE]
I'm sure I made something more confusing then it needs to be, or maybe messed something up; if so, please just tell me! :)
Sorry, you need to Log In to post a reply to this thread.