- Home /
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
}
}
Answer by Deadcow_ · Oct 23, 2014 at 06:22 AM
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
I tried to use this, but I'm hitting a wall. When I put the line "if(_isDead) return;" it tells me that "_isDead does not exist in the current context."
Then I tried to define _isDead and access it within 2 different scripts, and after about 2 hours of working in vain, I figured I should just ask for some clarification on that particular line...
I assume that you'll put there some check, that'll break jumps on some condition. _isDead is just a sample, use your own condition or simply remove this line to see if that code gives you what you want
It did! I think i was overthinking it. I wound up doing exactly what you suggested without realizing it... then i realized what you said! Thanks!
Cool :) Interesting to see how this jumps looks in your game in process) And if you solved your problem you may mark one of the answers as accepted to close this question
Answer by Dark_Tiger · Oct 23, 2014 at 06:08 AM
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
}
You used Random from System namespace, Unity offers it's own Random
class with nice static variables such as Random.value ins$$anonymous$$d of r.Next(1,100)
you used (r.Next(0,1) to be precise) and static function Range
that acts as .net Random.Next
function, but not requires to create instance of Range class
Oh cool, I'm pretty new to this so I'm just going off what I learnt making general applications in visual studio. Should pretty much take what I write as pseudo code xD
I appreciate the responses! Ill give these a shot and let you know!
Your answer
![](https://koobas.hobune.stream/wayback/20220613165722im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2d obstacle moving up and down question 1 Answer
Random Movement in 2D 2 Answers
2d Platformer Jumping Not Working 1 Answer
How to create random movement in 2d 2 Answers
Character doesn't jump repeatedly 1 Answer