How to Make Entity Spawn Where Player is Looking At
3 replies, posted
Hello, I'm currently making a gamemode and have a question. I am trying to make an entity spawn where the player looks as it will be an entity the player buys.
I have tried looking into other gamemode codes that have this to see IF I could figure it out but I am at a total loss.
Heres the code:
[CODE]
//spawn profitables
function BuyBronzePrinter( ply, cmd )
if ply:GetMoney() >= 2000 then
ply:TakeMoney( 2000 )
local pos = ply:GetPos()
bronze=ents.Create("money_printer_bronze")
bronze:SetPos(Vector(_ply:GetEyeTrace().HitPos.x, _ply:GetEyeTrace().HitPos.y, _ply:GetPos().z + 10))
bronze.Owner = ply
bronze:SetColor(205, 127, 50, 100 )
bronze:SetRenderMode( RENDERMODE_TRANSALPHA )
bronze:Spawn()
ply:ChatPrint("You got a Bronze Printer!") --print "You got a x!" in the chat
else
ply:ChatPrint("Not enough dosh for that one!")
end
end
concommand.Add("buybronzeprinter", BuyBronzePrinter)
[/CODE]
I can not figure this out at all, if one of you could help me It would be greatly appreciated. Thanks!
[code]
bronze:SetPos(Vector(_ply:GetEyeTrace().HitPos.x, _ply:GetEyeTrace().HitPos.y, _ply:GetPos().z + 10))
[/code]
Why do you do _ply instead of ply...
the underscore probably makes the language think you're referencing a nil value. Just try ply.
It helps if you post any errors you're seeing.
And no, not "probably" — it [i]is[/i] nil.
1) _ply is nil
2) bronze should be a local variable
3) localize the results of ply:GetEyeTrace() instead of calling it repeatedly
4) if you look at the structure for [url=http://wiki.garrysmod.com/page/Structures/TraceResult]trace results[/url], you see it has a HitPos field. use that.
5) you probably want to use a normal trace instead of ply:GetEyeTrace() unless you want players to be able to spawn money printers from across the map.
Again I was referencing code for the setpos, I'm not that familiar with Tracing so I'm really lost on how to handle this. I will look more into tracing. I'll get back to you on those errors
edit:
Finally Figured it out, Thanks for stearing me in the right direction!
Sorry, you need to Log In to post a reply to this thread.