Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to ensure spawns in an adjacent room?

Asked by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

hello everyone, I have a set of rooms from a top down perspective, and I want things to appear from the opposite side of each room's adjacent doors. since two adjacent rooms will be using the same door, I need to decide which side of the door an enemy will spawn, depending on the active room. I'd like to do this using simply the vectors of the active room and the door.

in this diagram attached, green would be the active room, red would be the doors, and blue would be the doors' look vectors (which I use to offset the spawn, added or subtracted from the door's position). the direction of the look vectors are arbitrary because rooms are interconnected. orange is where the enemy should spawn.

diagram

I am pretty new to vector math, right now I am simply calculating vectors between both sides of the door and the room and finding which has a larger magnitude. I am wondering if there is a more efficient way to do this?

function Entryway:Spawn(enemy, room)
    local base = self.Model.PrimaryPart
    local boundingBox, modelSize = enemy.Model:GetBoundingBox()
    local primaryOffset = enemy.Model.PrimaryPart.Position - boundingBox.Position -- offset from center of model
    local lookPos = base.Position + Vector3.new(0, (base.Size.Y + modelSize.Y) / 2, 0)

    -- relevant calculations here
    local offset = base.CFrame.LookVector * (base.Size + modelSize) / 2
    local front = lookPos + offset
    local back = lookPos - offset
    local spawnPos = (front - room.Model.PrimaryPart.Position).Magnitude > (back - room.Model.PrimaryPart.Position).Magnitude and front or back
    -- relevant calculations end

    enemy.Model:SetPrimaryPartCFrame(CFrame.lookAt(spawnPos, lookPos))
    enemy.Model:TranslateBy(primaryOffset)
    enemy.Model.Parent = workspace
end

thanks.

edit: this question probably isn't answered unless I say otherwise so feel free to answer

Answer this question