Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PRninja8488 · Oct 23, 2014 at 05:35 AM · 2dmovementrandomjumpinterval

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 
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PRninja8488 · Oct 24, 2014 at 04:30 AM 0
Share

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...

avatar image Deadcow_ · Oct 24, 2014 at 05:43 AM 0
Share

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

avatar image Deadcow_ · Oct 26, 2014 at 10:01 AM 0
Share

So, it helped?

avatar image PRninja8488 · Oct 26, 2014 at 12:14 PM 0
Share

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!

avatar image Deadcow_ · Oct 26, 2014 at 12:35 PM 0
Share

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

avatar image
0

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
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deadcow_ · Oct 23, 2014 at 06:33 AM 0
Share

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

avatar image Dark_Tiger · Oct 23, 2014 at 06:45 AM 1
Share

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

avatar image PRninja8488 · Oct 23, 2014 at 04:48 PM 0
Share

I appreciate the responses! Ill give these a shot and let you know!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges