Making enemies jump at random (c#)

Hey everyone! I’m making a stationary shooter (player is in a fixed position and the enemies move towards the player), and I’m trying to get the enemy objects to jump at random intervals. I’ve ripped through the scripting API and tutorials to find out how to do this, but I’ve had no luck. I’m hoping to use the jumpHeight variable to define how high they will jump (I would like this to be randomized to an extent), and the jumpTime variable to define the intervals for the jumps.

Here’s the script I have so far:

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {

public float speed;
public float jumpHeight;
public float jumpTimer;
public Rigidbody2D rigidbody2d;
	
void Start ()
{
	rigidbody2d.velocity = transform.right * speed;
}
void Update ()
{
	if 
}

}

If I understand your problem properly, I’d suggest to use coroutines for this:

IEnumerator JumpLogic()
{
    float minWaitTime = 5;
    float maxWaitTime = 10;

    while (true)
    {
        yield return new WaitForSeconds(Random.range(minWaitTime, maxWaitTime);
        if (_isDead) return;
        Jump();
    }
}

and launch this coroutine at start:

StartCoroutine(JumpLogic());

this will cause Jump function to be called every 5-10 seconds (while current enemy is alive).

If uou need random jump force you can solve it same way:

float minJumpForce = 100;
float maxJumpForce = 150;
...
float force = Ranfom.range(minJumpForce, maxJumpForce);
Jump(force);

Hope it helps

Try

Random r = new Random(); //This goes up with your other variables

//This stuff goes in your Update()

int randomNumber = r.Next(1,100);

//r<10 gives a 10% chance for them to jump, if you want it less/more just lower/raise the number

if(r < 10)
{
    //Do your jump stuff here
}