Jump to content

Spawning a prefab at another prefabs location.


Recommended Posts

Most of this is from the pond prefab. The problem with this code is it seems to spawn all instances of bamboo at the center of the world. I attempted to use GetWorldPosition and adding that to the location of the pond but it seems that isn't making the bamboo spawn near the pond.

local function AddBamboo(inst)  local spawns = {}  local pondx, pondz, pondy = inst.Transform:GetWorldPosition() -- this is my attempt at fixing the problem  for i = 1, math.random(3, 6)-3 do    local theta = math.random() * 2 * PI    table.insert(spawns,    {      offset =      {        (math.sin(theta) * 1.9 + math.random() * .3) + pondx,        0,        (math.cos(theta) * 2.1 + math.random() * .3) + pondy,      }    })  end  for i, v in pairs(spawns) do      if type(v.offset) == "table" and #v.offset == 3 then        local spawn = SpawnPrefab("bamboo")        if spawn ~= nil then          spawn.Transform:SetPosition(unpack(v.offset))        end      end  endend
Link to comment
Share on other sites

@Renarii,

here's how i spawn stuff near my player:

local pos = GLOBAL.Vector3(player.Transform:GetWorldPosition())    local offset = (GLOBAL.FindWalkableOffset(pos,math.random()*GLOBAL.PI*2,0.5,false))    if offset == nil then          if  player.components.talker then          player.components.talker:Say("No Space!", 2.5)          end          return    end    pos=pos+offset    local unit=GLOBAL.SpawnPrefab("spider")    unit.Transform:SetPosition(pos:Get())
also, use prints to log the values of the position of the pond and the values u use for SetPosition. maybe your maths dont work out the way u want^^ Edited by Seiai
Link to comment
Share on other sites

They in-fact do not, the positions that are being returned by inst.Transform:GetWorldPosition() is apparently 0,0,0 thus the spawning near the center of the world. I presume this is because I'm adding this o the PostInit for the pond prefab and at that stage it has yet to determine it's location.

 

I'm trying to find out where AddPrefabPostInit is declared and hoping there is a similar function that runs later after the ponds have a position.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...