Ever wondered how the Pikachu monster or NaliRabbits jump randomly? This tutorial will show you how to do that.
For this we will need to use the special Timer() function that can be set to call it self every x amount of time. This tutorial will show you how to take advantage of the RandRange() function that will allow us to randomize this jump feature as much as possible. First we need to declare some variables and set them in the defaultproperties.
var float MinJumpTime; var float MaxJumpTime; var float MinJumpHeight; var float MaxJumpHeight; defaultproperties { MinJumpTime=4.00 MaxJumpTime=10.00 MinJumpHeight=50.00 MaxJumpHeight=900.0 }
MinJumpTime is the minimum amount of time that must pass before the monster can jump again. MaxJumpTime is the maximum amount of time that must pass before the monster should jump again. MinJumpHeight is the minimum height of the jump the monster will do. And you guessed it, MaxJumpHeight is the maximum height of the jumps. Next we need to start the timer in a function that is called early in the monsters life. PostBeginPlay is good for this. We also need to up the timer. PostBeginPlay() is a very important function for our monster and we must make sure it is called in the parent class for our monster to work right. We do this by using the special Super command. Include it in the function just like in the following code.
function PostBeginPlay() { Super.PostBeginPlay(); SetTimer(1.00, false); }
In the PostBeginPlay function, it sets the Timer() to be called in 1 second and not repeat. This is because we will let the Timer() repeat itself at random. Add the following Timer code to your class.
function Timer() { JumpZ = RandRange(MinJumpHeight,MaxJumpHeight); DoJump(true); JumpZ = default.JumpZ; SetTimer(RandRange(MinJumpTime,MaxJumpTime),false); }
To make the monster jump we are calling a function called DoJump(); and change our monsters JumpZ value. After this the default JumpZ is restored so the monster jumps normally inbetween our special jumps. Then the timer calls itself again at a random time using the SetTimer() code. To make all this random we will utilize the RandRange() function. This function returns a random value between the first and second values we input.
Thats it! You might want to make things configurable such as the Min and Max JumpTimes and JumpHeights.
TIP – RandRange() can be used to set many random values.
Recent Comments