Dynamic ray range?

Is there a way of changing a ray sensors range with python?
otherwise, i will have to give it a really high value, then determine if the distance from target to emitter is in range… that’ll be a chore! THANKS!

You can change the range of a ray sensor using:

cont = logic.getCurrentController()
ray = cont.sensors['RaySensor']
ray.range = whatever

However, this method will only apply the range after the frame changes. To use a ray sensor dynamically (i.e. check a direction, then change the range and check it again), use the object’s rayCast function.

frame? what does that mean (thanks btw!)

I mean that changing the range value of a Ray sensor in Python won’t affect the sensor immediately. It will take one frame of gameplay for the change to take effect. What is a frame? A frame is a small timeframe in which the game updates all of the objects and redraws everything. Most games run at 60 frames per second, so 1 frame in this case is a 60th of a second in the game. If you change the range value of a ray sensor using Python, the value won’t do anything before the game resamples the sensor, 1 frame later (as is my understanding, you aren’t polling the sensor for updates, merely changing the range value). So, if you need to use multiple ray sensors with different distance values at once, it’s better to use the rayCast function.

that’s what i presumed, like a logic tick… it works btw!! thanks, it’s for my weapon switching