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 UniluckStudios · Nov 11, 2017 at 01:31 AM · camerainstantiatespawnspawningy-axis

Having Trouble with Instantiating an object on an axis

In my little project, I want to spawn spikes on the y-axis at a random position above the view of the main camera. I also want to spawn the object on the sides of the screen. So far, I have gotten the object to spawn, but it spawns in the middle of the screen and in the view of the camera instead of on the sides and above. here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnSpikes : MonoBehaviour 
 {
     Vector2 cameraPos;
     public GameObject twoSpikes;
     private Vector2 screenSize;
     Vector2 spawnPositions;
     Vector2 yValue;
     Vector2 xValue;
     Vector2 maxY;
 
     void Start()
     { 
         cameraPos = Camera.main.transform.position;
         xValue.x = cameraPos.x + screenSize.x;
         yValue.y = cameraPos.y + screenSize.y;
         maxY.y = yValue.y + 10;
         screenSize.x = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0))) * 0.5f; 
         screenSize.y = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height))) * 0.5f;
         spawnPositions = new Vector2(xValue.x, Random.Range(yValue.y, maxY.y));
         Debug.Log(screenSize.x + cameraPos.x); 
     }
 
     private void Update()
     {
         Instantiate(twoSpikes, spawnPositions, Quaternion.identity);
     }
 }
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
0
Best Answer

Answer by UniluckStudios · Nov 11, 2017 at 03:04 AM

Ok I figured it out. Here is my new code:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class SpawnSpikes : MonoBehaviour 
  {
      Vector2 cameraPos;
      public GameObject twoSpikes;
      private Vector2 screenSize;
      Vector2 spawnPositions;
      Vector2 yValue;
      Vector2 xValue;
      Vector2 maxY;
  
      void Start()
      { 
          cameraPos = Camera.main.transform.position;
          xValue.x = cameraPos.x + screenSize.x;
          yValue.y = cameraPos.y + screenSize.y;
          maxY.y = yValue.y + 10;
          screenSize.x = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0))) * 0.5f; 
          screenSize.y = Vector2.Distance(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)), Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height))) * 0.5f;
          spawnPositions = new Vector2(xValue.x, Random.Range(yValue.y, maxY.y)); //I changed this line
          Debug.Log(screenSize.x + cameraPos.x); 
      }
  
      private void Update()
      {
          Instantiate(twoSpikes, spawnPositions, Quaternion.identity);
      }
  }

I changed the line mentioned above to:

spawnPositions = new Vector2(xValue.x + (screensize.x 2) 0.5f, Random.Range(yValue.y, maxY.y));

//Now everything works

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
avatar image
0

Answer by michi_b · Nov 11, 2017 at 02:24 AM

so the first issue here is the order of execution. keep in mind, the start event is only called once. in that code, you first set xValue.x to cameraPos.x + screenSize.x, but at that point, you haven't written anything to screenSize yet, so it will have its default value (0, 0). therefore, that position will just be the camera's position. similar issue with the y coordinate, but at least the random offset starting from the camera position should work but the randomization only happens once at the start.

i don't really get what you want to achieve here, so i'll guess. A new instantiated spike at a fresh randomized position above the top left and top right corner of the camera at each frame? then the code should be something like this:

 using UnityEngine;
 
 public class SpawnSpikes : MonoBehaviour
 {
     public GameObject twoSpikes;
 
     private void Update()
     {
         Vector2 topLeftViewPortPosition = new Vector3(0f, Screen.height, 0f);
         Vector2 topRightViewPortPosition = new Vector3(Screen.width, Screen.height, 0f);
         InstantiateSpikeAtRandomHeight(topLeftViewPortPosition);
         InstantiateSpikeAtRandomHeight(topRightViewPortPosition);
     }
 
     private void InstantiateSpikeAtRandomHeight(Vector3 viewPortStartPosition)
     {
         Vector3 startPosition = Camera.main.ViewportToWorldPoint(viewPortStartPosition);
         Vector3 spawnPosition = startPosition + Vector3.up * Random.Range(0f, 10f);
         Instantiate(twoSpikes, spawnPosition, Quaternion.identity);
     }
 }

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 UniluckStudios · Nov 11, 2017 at 02:42 AM 0
Share

Ok, I tried your code and the spikes spawned somewhere, but I couldn't see them. The code also made my camera act weird. $$anonymous$$y game is a 2D game. So far, I have made a object that bounces around in the screen and goes up whenever I press the space bar. I have also made the camera's y position be equal to the object's y position. The problem I am trying to solve is making an object spawn at the right and left edge of the screen and above the camera's orthographic view so that the player cannot see the object being spawned. I can figure out the random positioning and stuff later, I just need/want to know how to make the object spawn in the correct area.

avatar image UniluckStudios · Nov 11, 2017 at 03:05 AM 0
Share

Thank you for your help though.

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

125 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

Related Questions

Instantiate is throwing my throwing my objects? 1 Answer

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

How to spawn 2 events with a score gap..? 0 Answers

How would I create a script that spawns objects more frequently as time goes on? 3 Answers

Prevent object from spawning at a spawn point twice in a row? 3 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