Jump to content

Recommended Posts

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 by SuperDavid

LqfEBdI.png

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.

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 :D!!

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 :D!!!

@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)".

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...