How to ensure spawns in an adjacent room?
Asked by
4 years ago Edited 4 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?
01 | function Entryway:Spawn(enemy, room) |
02 | local base = self.Model.PrimaryPart |
03 | local boundingBox, modelSize = enemy.Model:GetBoundingBox() |
04 | local primaryOffset = enemy.Model.PrimaryPart.Position - boundingBox.Position |
05 | local lookPos = base.Position + Vector 3. new( 0 , (base.Size.Y + modelSize.Y) / 2 , 0 ) |
08 | local offset = base.CFrame.LookVector * (base.Size + modelSize) / 2 |
09 | local front = lookPos + offset |
10 | local back = lookPos - offset |
11 | local spawnPos = (front - room.Model.PrimaryPart.Position).Magnitude > (back - room.Model.PrimaryPart.Position).Magnitude and front or back |
14 | enemy.Model:SetPrimaryPartCFrame(CFrame.lookAt(spawnPos, lookPos)) |
15 | enemy.Model:TranslateBy(primaryOffset) |
16 | enemy.Model.Parent = workspace |
thanks.
edit: this question probably isn't answered unless I say otherwise so feel free to answer