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 matterlinx · Oct 08, 2017 at 11:16 PM · instantiatespawnspawningspawnpoints

How do I spawn an object under another moving object at random in C#?

So I have an chicken moving left and right, and I want to spawn eggs at random times under it. How can I do this? I know the concept for doing it, that much is obvious; Get the location of the chicken. Instantiate an egg prefab with the same location of the chicken, just with a lower y value. Then just put it in an infinite loop with a random time delay... The problem is I have no idea how to do most of this because I am brand new to Unity.

If there is someone that could help me, that would be awesome!

By the way, the game I am making is rendered in 3D, but it is essentially a 2D game because the objects have restrictions in the x direction.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rocketman_Dan · Oct 09, 2017 at 05:17 AM

Sounds like conceptually you have it but need help learning the C# required?

For spawning an egg:

 public class EggDrop : Monobehaviour
 {
     public GameObject eggPrefab;    //this is your egg, assign it in the editor
     float dropDelay = 0;
     
     void Update()
     {
         if(dropDelay > 0)
         {
             dropDelay -= Time.deltaTime;
         }
         else
         {
             Instantiate(eggPrefab, transform.position, transform.rotation)
         dropDelay = Random.Range(1.0f, 5.0f);    //sets the time till next drop 
         }
     }
 }

For moving the chicken back an forth linearly you need a sin wave. Play around with the parameters for a better result, this is a a rough copy of what you'll need.

 public class ChickenMove : Monobehaviour
 {
     public float speed = 5;    //use this in the inspector to set the speed at which it moves
     void Update()
     {
         gameObject.transform.position.x = Mathf.Sin(Time.time * speed);
     }
 }

You can do mathematical operation on the right sight of that equation to change where it is. Make it a child of an empty gameObject if you want to more easily place it. That would mean it uses it's local position and should go from minus 1 to plus 1 in the x value where the parent object is. To get more than 1 unit worth of movement add a multiplier like Mathf.Sin(Time.time * speed) * 3; would make it go between -3 and +3.

If you don't understand any part of this just ask. It's better have it explained so you know why it works than to always have to ask with similar problems. Also point out any mistakes I made. I'm going from memory but the logic should be sound even if there's something I'm missing in the code. If there's a serious mistake let me know, I don't want to be spreading misconceptions, I have used the sine wave technique recently but on a scaling object, though the principle should be the same on transform positions, it's been a while since I actually did it for movement.

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

88 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

Related Questions

Prevent object from spawning at a spawn point twice in a row? 3 Answers

Storing the Position Generated by Random.Range 2 Answers

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

Object Spawning Randomly 0 Answers

What's wrong with my script? 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