Setting position from a range

I have a game object that is meant to stay in a specific position if it is let go in a range around that position. This is the code at the moment but I can’t seem to use the > or < symbols for positions.

Screenshot 2023-01-04 152209

I would like to know what I’m doing wrong, and if this is something that I can achieve in the first place. Thank you.

You can’t really compare vector3 values that way, but let’s imagine you could, then the code would look like this:

if pos >= vmath.vector3(...) or pos <= vmath.vector3(...) then
   print("foobar")
end

So how to really solve this? It looks like you want to reset the position if outside of a 20 by 20 pixel box centered on 880, 585. One option is to compare each component individually:

if pos.x >= 890 or pos.y >= 595 or pos.x <= 870 or pos.y <= 575 then
   print("foobar")
end

Another option is to first calculate the difference between the position and the center:

local diff = pos - vmath.vector3(880, 585, 1)
if math.abs(diff.x) >= 10 or math.abs(diff.y) >= 10 then
   print("foobar")
end

If you are ok with it being a circle instead of a box then calculate the length/distance from the center instead:

local length = vmath.length(pos - vmath.vector3(880, 585, 1))
if length >= 10 then
   print("foobar")
end
5 Likes

Thank you. The game is looking a lot better now :grin: