• 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 blythethompson · Apr 03, 2016 at 04:08 AM · c#2d gamespawnspawningrandomspawning

2d spawn across screen

Hi! I'll just start by saying that I am a complete nooby moron when it comes to programming (art i can do, code not so much) but i'm working on a small 2d mobile game for a uni project.

At this point i'm trying to figure out how to spawn, say, 3 random sprites off screen, and have them go across the screen, from right to left, i have no code to show because everything I've tried so far hasn't worked and is undoubtedly wrong, i'd rather start from scratch.

These three objects will be fish, two being worth points, one poison that kills the player if they catch it (via their shark avatar on the left side of the screen)

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 Vallar · Apr 03, 2016 at 07:45 AM

Hello,

I am a bit new myself however, you can do the below as an example (since there isn't too much details I am guessing half of what is needed).

To create the object off screen, check the coordinates on the point you want to spawn at (for this example let's say x = -4.62, y = 2 and since it is 2D so z = 0.

To spawn in C# you use the Instantiate keyword and provide a prefab name. To move there are a few methods we'll use MoveTowards as it is easy.

Our code would look similar to the below: public GameObject fishPrefab; public float fishSpeed;

 private Vector3 spawnPosition = new Vector3 (-4.62f, 2, 0);
 private Vector3 position1 = new Vector3 (2, 2, 0);
 
 
 void Start()
 {
 // To Spawn:
 GameObject fish = Instantiate(fishPrefab, spawnPosition, Quaternion.identity) as GameObject;
 
 // To move:
 fish. transform.position = Vector3.MoveTowards(fish.transform.position, position1, Speed * Time.deltaTime);
 }

To make it random you can add the below:

 public GameObject[] fish;
 private int randomPicker;
 private Vector3 spawnPosition = new Vector3 (-4.62, 2, 0);
 private Vector3 position1 = new Vector3 (2, 2, 0);
 void Start()
 {
 
 
 for (int i = 0; i < 3; i++)
 {
 randomPicker = Random.Range(1, 4);
 
 switch (randomPicker)
 {
 case 1:
 GameObject fish1 = Instantiate(fish[1], spawnPosition, Quaternion.identity) as GameObject;
 fish1. transform.position = Vector3.MoveTowards(fish1.transform.position, position1, Speed * Time.deltaTime);
 break;
 case 2:
 GameObject fish2 = Instantiate(fish[2], spawnPosition, Quaternion.identity) as GameObject;
 fish2. transform.position = Vector3.MoveTowards(fish2.transform.position, position1, Speed * Time.deltaTime);
 break;
 case 3:
 GameObject fish3 = Instantiate(fish[3], spawnPosition, Quaternion.identity) as GameObject;
 fish3. transform.position = Vector3.MoveTowards(fish3.transform.position, position1, Speed * Time.deltaTime);
 break;
 }
 }
 }
 

Hope that helps.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Spawn after checking if the clones are destroyed. 1 Answer

When spawning to a grid, how to only spawn one object next to another? 1 Answer

player spawning 0 Answers

How to make my enemies spawn at spawn point, but not randomly? 1 Answer

Enemies spawn on top of each other(C#)(Unity) 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