Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Airmand1 · Jul 25, 2014 at 07:32 AM · vector3randomspawnrandom.range

Using Vector3 in ViewportToWOrldPoint

I have a script that spawns spawn points. What I am trying to make happen is that a spawn point will spawn randomly (x,y) just off the screen.

The character in this small game doesn't move but can rotate 360 degrees and enemies can randomly come at him from any direction. Im stuck here. The same thing happens everytime and its not random. I get spawners that spawn just outside the top and spawners that appear to spawn right above my character. Im obviously doing something very wrong.

 public class SpawnPointController : MonoBehaviour {
     
     //array of our spawn points
     public GameObject[] SpawnPoints;
     
     //variables for spawn delay
     private float nextSpawn = 0;
     public float spawnRate = 2;
     
     //amount of spawners
     public int SpawnAmount = 5;
     
     //spawner prefab
     public GameObject SpawnPointPrefab;
     
     
     //Spawn Point Position
     public Vector3 v3Pos = new Vector3 (Random.Range(-.15f,1.15f), Random.Range(-.15f,1.15f), 19);
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         SpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
         v3Pos = Camera.main.ViewportToWorldPoint(v3Pos);
         for (int i=0; i<SpawnAmount; i++)
         {
             if (Time.time  > nextSpawn && SpawnAmount > 0)
             {
                 nextSpawn = Time.time + spawnRate;
                 GameObject pos = SpawnPoints[Random.Range (0, SpawnPoints.Length)];
                 Instantiate(SpawnPointPrefab,v3Pos, Quaternion.identity);
                 SpawnAmount--;
             }
         }
     }
 
 }
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
Best Answer

Answer by robertbu · Jul 25, 2014 at 08:01 AM

I spot three problems and a potential problem right away. First this line:

 public Vector3 v3Pos = new Vector3 (Random.Range(-.15f,1.15f), Random.Range(-.15f,1.15f), 19);

Does not create a spawn point off the screen. For example, it could return (0.5, 0.5) and be in the middle of the screen.

Problem #2, is that you only initialize v3Pos to a viewport coordinate at the top of the file, and that initialization is only done once when the script is attached to the game object.

On line 28, you convert v3Pos to a world coordinate, but subsequent passes through Update() are passing a world coordinate to ViewportToWorldPoint().

Might I suggest the following for the generation of spawn points just outside the screen.

 v3Pos = new Vector3(0.857f, 0.857f, 0.0f);
 v3Pos = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), Vector3.forward) * v3Pos;
 v3Pos += new Vector3(0.5f, 0.5f, 19.0f);
 v3Pos = Camera.main.ViewportToWorldPoint(v3Pos);

v3Pos could be local to Update() and not a public class instance variable. This code works by creating a Vector the length from the center of the screen to just off the top right corner of the screen. Then it randomly rotates that vector and add it to the center of the screen (Viewport coordinate). Since the potential spawn points are in a circle around the origin, the spawn points will be a litte bit further from the screen when spawned on the middle of the edge. Not sure if that is a problem for you or not.

Comment
Add comment · Show 3 · 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 Airmand1 · Jul 25, 2014 at 08:30 AM 0
Share

Thanks that worked

avatar image Bunny83 · Jul 25, 2014 at 09:25 AM 0
Share

Valid viewport coordinates are between 0 and 1 where (0, 0) is the left bottom screen point and (1, 1) the top right. Using values greater than 1 or smaller than 0 might result in strange results depending on the projection used.

Also keep in $$anonymous$$d that a screen usually is wider than tall, so your circle becomes an ellipse.

avatar image robertbu · Jul 25, 2014 at 01:05 PM 0
Share

@Bunny83 - Can you explain under what conditions the Viewport coordinates fail? So you are saying the OP should not solve this problem this way but ins$$anonymous$$d do something else...like use viewport coordinates to find the center and edges and then use world space coordinates to move beyond the edges?

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

2 People are following this question.

avatar image avatar image

Related Questions

How To Random Range a Float? 2 Answers

Spawn object from Random Vector3 in array 0 Answers

Random.Range Vector3 2 Answers

random place generator 1 Answer

How are vector3s instantiated? 2 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