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 wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 18, 2018 at 07:28 AM · unity 2drespawningrandom spawn

Random Respawn,Random Spawn

Currently, I'm making a shooting game. I want to respawn enemies randomly. This is the code i've made:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RespawnManager : MonoBehaviour {
 
     public GameObject obj;
     public Transform respawnTr;
 
     void Start () {
         StartCoroutine(RespawnEnemy());
     }
 
     IEnumerator RespawnEnemy ()
     {
         while (true) {
             yield return new WaitForSeconds (0.7f);
             float range = Screen.width / Screen.height * Camera.main.orthographicSize;
             float random_num = Random.Range (-range, range);
             Instantiate (obj, respawnTr.position + new Vector3(random_num, 0, 0), Quaternion.identity);
         }
     }
 }

However, when I run the program, the enemies just respawns at one place. How should I solve this problem????

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 tormentoarmagedoom · Jun 18, 2018 at 07:41 AM

Good day.

I see your position random is based on screen with, size and orthographic size.

I dont know whats the result of "float range = Screen.width / Screen.height * Camera.main.orthographicSize;"

Can you put this 2 lines right before the Instantiate line and say us what you get in the Console?

 Debug.Log ( "range = " + float range = Screen.width / Screen.height * Camera.main.orthographicSize);
 Debug.Log (float random_num = Random.Range (-range, range));

But, I recommend to don't use this to make a random position. is better if you do random range from numbers, like:

 float random_num = Random.Range (-20, 20);

Bye!

Comment
Add comment · Show 1 · 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 wechat_os_Qy04hwWiqsL9Ov94DYr-DvD6Q · Jun 18, 2018 at 03:57 PM 0
Share

alt text

Hmm... I've got 0 for my values.... How should I fix this problem?? Thank You :)

check.png (31.6 kB)

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

86 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

Related Questions

animate two character at the same time in unity 2d 0 Answers

Detecting when a 2D object passes a point on a consistent basis 0 Answers

Unity Editor not loading/working 2 Answers

How do I get the object from an adjacent space 2d grid 0 Answers

Replicating MC Redstone Wiring 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