Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unity_5120bee · Oct 17, 2017 at 03:19 AM · unity 2drandom.rangesidescrollermathf.sin

Generating enemy that moves in Sine but want to randomize height?

I have one line of code that I've been stuck with on how to code it properly below:

 currentPosition.y = Mathf.Sin(Time.time * 10f) * 100f + y;

where currentPosition is a Vector2 type variable and y is a float.

My game is a 2D sidescroller and I want my enemies (birds) to fly in a Sine motion but I want to change the height that they spawn at to Random height between -400f to 400f.

If it do a single fixed number like y = 100f, the bird enemy flies in a straight direction with a Sine motion (up and down) but it is always being spawned at that fixed height (100f).

If I use Random.Range(-400f, 400) the bird enemy will just erratically spaz between the top and bottom edges of the screen rapidly (400 max to -400 min).

How can I modify my code above so that it randomly generates the enemy along the y-axis between -400f and 400f without it spazzing out?

Comment
Add comment · Show 1
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 unity_5120bee · Oct 17, 2017 at 03:21 AM 0
Share

EDIT: to avoid confusion, I've renamed my y float variable to "height"

1 Reply

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

Answer by NoseKills · Oct 17, 2017 at 06:24 AM

If your bird "erratically spazzes", I guess you must be calling Random.Range() in Update giving the bird a new base height every frame?

Pick a random y value in a method that gets called only once , like Start or Awake, and offset your bird in relation to that base height with the sine formula.

 float y;
 void Start(){
     // Without the 'f' at the end of 400 you would be calling the integer version of Random.Range(), which only returns integer values
     y = Random.Range(-400f, 400f);
 }
 
 void Update()  {
      currentPosition.y = Mathf.Sin(Time.time * 10f) * 100f + y;
 }
Comment
Add comment · 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

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

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hey! Im a beginner and need help in randomizing. (Im making a 2d game in Unity) 1 Answer

Optimizing Code 1 Answer

how can i random a spawning but not same gameobject 1 Answer

How to Make Character Shoot Where Aiming with Mouse 0 Answers

C# Script help with 2D sidescroller 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