unity – I’m trying to write a randomly patrolling AI for my 2D Platformer


I’m trying to write a randomly patrolling AI for my 2D Platformer. The AI already has a ground checker function which checks if there are tiles nearby or not. What I want to do is randomize its actions and create an illusion of a somewhat “sentient” enemy. What I tried to create below is using the built in RNG to make the enemy either jump, change direction, or keep moving.

The problem is, it doesn’t seem to work properly. The enemy just jumps every second I want it to change behaviour. The change direction functions, however, don’t occur as frequently. I need to know what I’ve done wrong here. Thanks.

void Update()
{   
      //RNG
      behaviour = Random.Range(0,3);
      jumpSpeed = Random.Range(1,5); 

      //clock
      timer += Time.deltaTime;
}

  //time
    private float waitTime = 2.0f;
    private float timer = 0.0f;

 void MoveRandomizer()
{
    if((Mathf.Round(timer%waitTime)) == 0)
    {
        if(behaviour == 0)
        {
        movingRight = false;
        
        }
        if(behaviour == 1)
        {   
        movingRight = true;
        
        }
        if(behaviour == 2)
        {
        rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
        }
    }   
    
}