Home   Help Search Login Register  

Author Topic: Setpos Random  (Read 461 times)

0 Members and 1 Guest are viewing this topic.

Offline Pilot

  • Contributing Member
  • **
Setpos Random
« on: 15 Feb 2005, 02:09:07 »
I have a script that creates a munition, and I want that munition placed randomely within a certain area, I have tried this:
Code: [Select]
_cluster1 setpos [(getpos _bomb select 0) - random 20, (getpos _bomb select 1) - random 5, (getpos _bomb select 2)]but it doesn't work, any suggestions?

Offline Tyger

  • Former Staff
  • ****
  • I was at OFPEC when it still had dirt floors...
    • OFPEC
Re:Setpos Random
« Reply #1 on: 15 Feb 2005, 03:31:54 »
check out Johann's tute on scripting here, he has several one like that in it I believe.

http://www.ofpec.com/editors/browse.php?category=1_3
"People sleep soundly at night only because rough men stand ready to do violence on their behalf." - George Orwell

MSG Mike Everret - We Will Never Forget - '75-'08

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Setpos Random
« Reply #2 on: 15 Feb 2005, 03:41:44 »
Student pilot, this script will always place the munition to the left and below (looking at the map) the main munition, _bomb1.
Since random(x) always returns a positive value from 0 to x, try this line  instead to get random placement in all directions:

 _cluster1 setpos [(getpos _bomb select 0)+20*(random(2)-1), (getpos _bomb select 1)+5*(random(2)-1), (getpos _bomb select 2)]

Since 'random(2)' returns values between 0 and two, (random(2) - 1) returns values between -1 and 1. Therefore placement in the 'x' direction occurs between 20 metres infront and -20 metres behind _cluster1, likewise with the 'y' direction betw. -5 and 5.
« Last Edit: 15 Feb 2005, 03:42:52 by Fragorl »

Offline Pilot

  • Contributing Member
  • **
Re:Setpos Random
« Reply #3 on: 15 Feb 2005, 05:56:25 »
Thanks for the replies, Tyger and Fragorl.  It turns out I had another problem with my script, so the random was working the whole time. ::)  Thanks especially for your suggestion Fragorl!  I didn't know what you suggested could be done, it will make my job much easier!

Topic Solved