![]() |
My fish keeps drowning when it reaches the center of water. - Printable Version +- 2DWorlds Forums (http://2dworlds.buildism.net/forum) +-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4) +--- Forum: Scripting (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=13) +--- Thread: My fish keeps drowning when it reaches the center of water. (/showthread.php?tid=8712) |
My fish keeps drowning when it reaches the center of water. - Qwertygiy - 12-26-2011 It correctly moves the fish back to water when it isn't in water (and there's water in the world for it to swim to), but whenever it reaches near the X-center of the water, fish Wrote:1 (1 is the number of Water blocks in the world.) The function that drives me nuts is iNeedTheWater: [lua] local scented = false function find() local players = game.Players:getChildren() local closest = nil local distance = 25 --Feel free to change this value; it's how far away they can "smell" players for q = 1, #players do if players[q].Character and players[q].Character:getChild("Body") then if players[q].Character.Body.Position ![]() closest = players[q].Character.Body distance = players[q].Character.Body.Position ![]() end end end if iNeedTheWater() == false then if closest then swim(closest.Position) scented = true else swim(Vec2D(math.random(script.Parent.Position.x - 5.01), math.random(script.Parent.Position.x + 5.01))) scented = false end else swim(findTheWater().Position) end end function swim(target) local ecks = 0 local why = 0 local vel = script.Parent:getChild("VelocityController") if target.x > script.Parent.Position.x then ecks = 3 script.Parent:getChild("FISHEEE").FlipX = true else ecks = -3 script.Parent:getChild("FISHEEE").FlipX = false end if target.y > script.Parent.Position.y then why = 1.5 else why = -2 end vel.Velocity = Vec2D(ecks, why) end function checkForWater(parental, tabull) local items = parental:getChildren() for q = 1, #items do if items[q]:isA("Water") then table.insert(tabull, items[q]) else tabull = checkForWater(items[q], tabull) end end return tabull end function findTheWater() local waterz = checkForWater(game.World, {}) if #waterz > 0 then print(#waterz) local closewater = nil local nearest = 1000 for e = 1, #waterz do if waterz[e].Position ![]() closewater = waterz[e] nearest = waterz[e].Position ![]() end end if closewater then return closewater end end end function iNeedTheWater() local watery = findTheWater() local dying = false if watery then if script.Parent.Position.x > watery.Position.x and script.Parent.Position.x - watery.Position.x >= 5 then if script.Parent.Position.x - (watery.Size.x * 0.5) > watery.Position.x then dying = true end elseif script.Parent.Position.x < watery.Position.x and watery.Position.x - script.Parent.Position.x >= 5 then if watery.Position.x - (watery.Size.x * 0.5) < script.Parent.Position.x then dying = true end end if script.Parent.Position.y > watery.Position.y and script.Parent.Position.y - watery.Position.y >= 5 then if script.Parent.Position.y - (watery.Size.y * 0.5) > watery.Position.y then dying = true end elseif script.Parent.Position.y < watery.Position.y and watery.Position.y - script.Parent.Position.y >= 5 then if watery.Position.y - (watery.Size.y * 0.5) < script.Parent.Position.y then dying = true end end if dying == true then print("FISH IS DYING GLARGLEPTHDDD") return true else return false end end end while true do if scented then sleep(0.05) else sleep(0.5) end find() if script.Parent:getChild("FISHEEE").Image == "http://i41.tinypic.com/35crcbs.png" then script.Parent:getChild("FISHEEE").Image = "http://i42.tinypic.com/25qg9yb.png" else script.Parent:getChild("FISHEEE").Image = "http://i41.tinypic.com/35crcbs.png" end end [/lua] RE: My fish keeps drowning when it reaches the center of water. - Derpy-Hooves - 12-27-2011 if you're going for what i think you are, and i'm smart... function iNeedTheWater() kungpao = checkForWater(game.World, {}) ded = false for i = 1, #kungpao do posi = script.Parent.Position if posi.x > kungpao[i].Position.x and posi.y > kungpao[i].Position.y and posi.x < kungpao[i].Position.x + kungpao[i].Size.x and posi.y < kungpao[i].Position.y + kungpao[i].Size.y then --sweet jesus have mercy ded = false else ded = true print("medic!") end end change this to your liking, and for the love of god, don't make angled water |