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 /
  • Help Room /
avatar image
0
Question by LAKSHAYMAVIA · Aug 30, 2018 at 12:21 PM · instantiateunity 2dmathf

I want to generate obj at random places which can move in random speed in X axis.,How to generate a object at random position with varying speed.

Hey Guys, I am working on something. In this project i want to generate a object at random position which can go left to right continuously. So far I have got this. This is inside start of a class that generate 50 object on which player can jump on when game loads.

 void Start ()
 {
     Vector2 pos = transform.position;
 
     for (int i = 0; i < noOfHolder; i++)
     {    
         pos.x = Random.Range(-3, 3);
         pos.y += Random.Range(1, 5);
         Instantiate(holder, pos, Quaternion.identity);
     }


and the holder object which is attached to it have a method.

 updatePosition()
 {
     startingPos.x = moveSpeed * Mathf.Sin(Time.time);//*moveSpeed
     transform.position = startingPos;
 }


This code can generate multiple objects. but with the same sin speed from left to right. how can i vary there X speed. Thank you for answering.

Comment
Add comment · Show 3
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 eses · Aug 30, 2018 at 12:27 PM 0
Share

@LA$$anonymous$$SHAY$$anonymous$$AVIA - could you please format you code again... half of each code block is part of previous text blocks... leave enters after each text line, to prevent this.

avatar image LAKSHAYMAVIA · Aug 30, 2018 at 01:59 PM 0
Share

@eses-Thanks. Does the code is formatted now.??? I tried. One thing eses

i change the move speed. Some obj move to the half of the screen some full. I want all the object move to full screen with vary speed. I added a random range to moveSpeed.

avatar image eses LAKSHAYMAVIA · Aug 31, 2018 at 11:16 AM 0
Share

@LA$$anonymous$$SHAY$$anonymous$$AVIA - I edited your post code blocks. What do you mean by "same sin speed" - you mean all your spawned objects move in similar manner?

If this is the case, you'll just have to offset the time value of sin for each object.

1 Reply

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

Answer by eses · Aug 31, 2018 at 02:16 PM

Hi @LAKSHAYMAVIA

The question is bit unclear, but if you want to modify the object positions using sin values, so that your objects don't move in same sync, you could do something like this in your instantiation:

 var h = Instantiate(holder, pos, Quaternion.identity);
 h.GetComponent<Holder>().offset = Random.Range(0f,3f);


And something like this in your Holder:

 startingPos.x = moveSpeed * Mathf.Sin(Time.time + offset);
 transform.position = startingPos;


With offset variable you can create movement similar to this:

alt text


Multiplying (like your line end comment shows) sin result only modifies the movement, but not it's timing.


20180830-sin-offset.gif (224.0 kB)
Comment
Add comment · Show 2 · 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 LAKSHAYMAVIA · Sep 01, 2018 at 12:12 PM 1
Share

@eses Hi Great man, Really love it... Thanks a lot. It really working great. I need to ask 2 more things....:)

avatar image LAKSHAYMAVIA · Sep 09, 2018 at 03:57 PM 0
Share

Thank you so much for this. With your help I am able to Complete my first game. I don't have any other way to contact you so . Thank you . I am attaching the link where i have mention you name hope you don't $$anonymous$$d. https://gamejolt.com/games/myWorld/366507

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

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

How to instantiate a prefab with an ID? 1 Answer

Firing to a mouse position 0 Answers

Referencing a script of a parent of a instantiated instance from another instantiated instance 0 Answers

Rigibody Velocity Wont Work? 1 Answer

All instantiated objects having same location, 0 Answers


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