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 gibsorya · Oct 08, 2019 at 06:46 PM · 3dspawning

Getting objects to spawn in front of the player outside of their view - Unity 3D

So I'm working on this project that more or less acts as an infinite runner. The player runs down a path, and moving obstacles will appear walking up and down the same path. Right now I'm not focusing on the objects moving, though... just having them spawn into the world.

The game is somewhat of an open world, so the player may be going up and down the same path multiple times, and sometimes the player will be taking right or left turns onto different paths.

I tried using the Camera.main.ScreenToWorldPoint method, and the objects can spawn in front of the player (if I place in the "right" coordinates, but I can't get the objects to actually spawn on the path. When they do spawn, they are usually above or below the path, and the spawn points don't move when the player moves.

Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NPCSpawner : MonoBehaviour
 {
     [SerializeField]
     private GameObject NPC;
     [SerializeField]
     private float respawnTime = 1.0f;
     [SerializeField]
     private Vector3 spawnPoint;
 
     private Vector3 screenBounds;
     
 
     // Start is called before the first frame update
     void Start()
     {
         screenBounds = Camera.main.ScreenToWorldPoint(spawnPoint);
         StartCoroutine(NPCWave());
     }
 
     IEnumerator NPCWave()
     {
         while (true)
         {
             yield return new WaitForSeconds(respawnTime);
             spawnNPC();
         }
     }
 
     void spawnNPC()
     {
         GameObject n = Instantiate(NPC) as GameObject;
         n.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y);
     }
 }
 



What am I doing wrong? Here is a screenshot of what the player can see: Here is a screenshot of what the player can see Also I'm still working with the camera angle, but essentially this will be the camera angle.

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
1

Answer by tormentoarmagedoom · Oct 08, 2019 at 07:08 PM

Hello There.

I dont understand exatcly tour problem. You mean the objects apear at right position in 2 axis, but they are too much over/under the "ground" ?

I see your code and saw this:

 n.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y);

You are creating a Vector3 but only giving 2 coords:

   x = Random.Range(-screenBounds.x, screenBounds.x)
   y = screenBounds.y

And Z ??

Something like this will work:

 n.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y, Player.transform.position.z);

(You need to reference correctly the player object)

Good luck!

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 gibsorya · Oct 08, 2019 at 08:03 PM 0
Share

I forgot to mention that I had messed around with the z coordinate in that Vector 3. Having the z coordinate spawned the objects above the player, not in front and above the player which happened with just the x and y coordinates.

So the issue is that the objects spawn way over the ground or way under the ground. If they were above the ground, the player will see the shadows once they go to that object.

I didn't think of doing Player.transform.position.z. I'll try that out! Thanks for the help

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

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

Problem with the randomly spawning enemies 0 Answers

How to spawn random buildings 5 Answers

How do I make enemies not spawn in the same position? 2 Answers

How to respawn player car in war track 1 Answer

Spawning Terrain Around The Player 0 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