. . . Posted December 20, 2017 Share Posted December 20, 2017 (edited) Hello, if someone can help me with this that'd be great! I want to spawn an item right behind my character, it has to be exactly behind them or it's just not right.. I'm using this code, but it only makes certain angles look like it spawns behind them. local x, y, z = inst.Transform:GetWorldPosition() SpawnPrefab("rainbow_fx").Transform:SetPosition(x-0.1, y, z) Edited December 27, 2017 by SuperDavid Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/ Share on other sites More sharing options...
CarlZalph Posted December 20, 2017 Share Posted December 20, 2017 entity.Transform:GetRotation() returns a value from [-180,180). This is not a normally math aligned value to the world axii- the vertical axis is flipped. So to get an offset from someent: local x, y, z = someent.Transform:GetWorldPosition() local rotation = (someent.Transform:GetRotation() + 180) * DEGREES x = x + math.cos(rotation)*radius z = z - math.sin(rotation)*radius Note the "DEGREES" constant defined in the global table is used to transform the degrees value into radians that math.(sin/cos) expect. Second note is that the math.sin is being subtracted- this flips the vertical component to be a proper aligned value to the world axii. Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/#findComment-986026 Share on other sites More sharing options...
. . . Posted December 21, 2017 Author Share Posted December 21, 2017 Here's the code now & here's the crash I get "variable 'radius' is not declared" local x, y, z = act.target.Transform:GetWorldPosition() local rotation = (act.target.Transform:GetRotation() + 180) * DEGREES x = x + math.cos(rotation)*GLOBAL.radius -- crash z = z - math.sin(rotation)*GLOBAL.radius SpawnPrefab("rainbow_fx").Transform:SetPosition(x, y, z) thanks for your help !! Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/#findComment-986160 Share on other sites More sharing options...
CarlZalph Posted December 22, 2017 Share Posted December 22, 2017 Because that's how far 'back' you want the thing to be. You choose this number. Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/#findComment-986700 Share on other sites More sharing options...
. . . Posted December 22, 2017 Author Share Posted December 22, 2017 1 hour ago, CarlZalph said: Because that's how far 'back' you want the thing to be. You choose this number. I don't understand? I have to replace radius with a number? Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/#findComment-986728 Share on other sites More sharing options...
. . . Posted December 27, 2017 Author Share Posted December 27, 2017 Well the code works just had to replace radius with a number like you said but I didn't understand for a couple days like an idiot hahaha, thanks CarlZalph!! Maybe you can help me one more time this code you gave me? It indeed does spawn the prefab always behind the character, but it spawns behind the character whichever way they're facing. What I meant by behind character is like always behind the character's body as if they're facing the player, is there a way to edit this great code you provided to do that? I'm very bad with math & don't understand coding really well so I have no idea what to do to make it like that if it's possible, thanks again !!! Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/#findComment-987742 Share on other sites More sharing options...
CarlZalph Posted December 27, 2017 Share Posted December 27, 2017 @SuperDavid For using the camera's perspective as the angle on the server, you'd need to network it to the server using an RPC call. Could trigger the client telling the server its camera angle by using a netvar and having its ondirty call the RPC once it's received, though with all things networking you'll have a delay between you wanting the prefab created and when it appears. If this is all done on the client-side and making a client-side entity then it'll be immediate and all things cherry. The math itself is the same if you neglect the pitch part of the camera and only use the yaw. It becomes a little bit more complicated when you take into account the pitch but not by much, just another trig function to create the projection. If you're trying to spawn a particle FX that always goes behind the player then you may want to look into "AnimState:SetLayer(LAYER_WORLD_BACKGROUND)". Link to comment https://forums.kleientertainment.com/forums/topic/85630-solved-how-to-spawn-an-item-right-behind-the-player/#findComment-987759 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now