r/CodingHelp • • Feb 27 '26

[Javascript] trying to make a line of sight

function lineOfSightUpdater(lineOfSight) {
  let currentLineOfSight = lineOfSight.width
  for (let i = 0; i < mainTerrain.length; i+= 1) {
   
 
  if(lineOfSight.crashWith(mainTerrain[i])){
lineOfSight.width = 10
  }
  else if(!lineOfSight.crashWith(mainTerrain[i])) {
if(lineOfSight.width < 300) {
lineOfSight.width = lineOfSight.width + 5
}
  }
   }
console.log(lineOfSight.width)
}function lineOfSightUpdater(lineOfSight) {
  let currentLineOfSight = lineOfSight.width
  for (let i = 0; i < mainTerrain.length; i+= 1) {
   
 
  if(lineOfSight.crashWith(mainTerrain[i])){
lineOfSight.width = 10
  }
  else if(!lineOfSight.crashWith(mainTerrain[i])) {
if(lineOfSight.width < 300) {
lineOfSight.width = lineOfSight.width + 5
}
  }
   }
console.log(lineOfSight.width)
}
ok, so what this does is it increases the size of the line of sight by 5 every 20 milloseconds until it reaches 300, but if it touches a piece of terrain it resets back to 0, however, for some reason, the amount it increases by gets bigger and bigger (should stay at 5) anyone have any idea why this happens?

1 Upvotes

3 comments sorted by