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 nickkorage · Nov 23, 2016 at 11:44 AM · instantiatetransform.positionspawning

Game objects are getting instantiated at the same position.

So my player is moving along the Y axis downwards but not at a constant or predictable rate (meaning the player can even stop moving ). The camera is following the player so I decided to use it to set a location for spawning game objects along the y axis. I used a Yoffset to make it spawn objects ahead of the camera and a DifferenceInDistance variable to try and maintain a distance between game objects, however, I'm still having game objects appearing at the same position. Please Help thanks :)

 public class ObjectSpawnner : MonoBehaviour {
     
     public float Yoffset;
 
 
     public GameObject [] obstacles;
 
     public int NumberOfGameObjects;
     public float startTime;
     public float RepeatRate;
 
     public float DifferenceInDistance = 4;
 
 
 
     float YDistance = 0;
 
 
     // Use this for initialization
     void Start () {
         InvokeRepeating ("GenerateRandomObjects",startTime ,RepeatRate );
     
     }
    
 
 
     void GenerateRandomObjects(){
         float x =obstacles [0].transform.position.x;
         float y = GameObject.Find ("Main Camera").transform.position.y;
         int r = Random.Range (0,NumberOfGameObjects);
 
         YDistance += DifferenceInDistance;
         Debug.Log ("Y distance is " + YDistance );
 
         Instantiate (obstacles [r], new Vector3 (x, y - Yoffset - YDistance, 0), Quaternion.identity);
     }
 
 }
 

Comment
Add comment · Show 4
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 Mister-Mortal · Nov 23, 2016 at 01:53 PM 0
Share

@nickkorage What do you mean they spawn at the same position? Are they spawning at the same xy coordinates? Or do you mean that they always spawn with same distance from each other?

avatar image nickkorage Mister-Mortal · Nov 24, 2016 at 06:06 AM 0
Share

$$anonymous$$y question simply is how do I spawn game objects while maintaining a certain distance from each other? Some instances in my game I'm finding the game objects being spawned together or not keeping the distance I intended from each other.

avatar image hexagonius · Nov 23, 2016 at 09:32 PM 0
Share

also not sure. from the looks YDistance is an ever increasing value while yOffset is an unknown value and both get subtracted from the current camera y. moving the camera down it should instantiate objects ever further away from it including it's speed. If it stays and only the character moves down, they should appear along his easy. that's how I read this, but I would like to hear the answer to $$anonymous$$ister-$$anonymous$$ortal's question too

avatar image nickkorage hexagonius · Nov 24, 2016 at 06:02 AM 0
Share

The player is not moving at a specific rate. The player's movement is unpredictable and the camera is following the player. I wanted the game objects to be spawned separately from each other maintaining a certain distance from each other but it's not happening. In some few cases I found the game objects (Which are not moving in any direction) spawning together or near each other. I can't account for this discrepancy.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Nov 25, 2016 at 10:17 AM

So I ran your code as is and it works as I described, spawning cubes all the way downwards without any hickups.
Spawned cubes The only fact that this could fail is that it constantly uses the Main Camera position.y and you said you were moving it, but then only if the camera goes up, since the offset extends down. I'd put the spawner on a gameobject that stands still and starts in the same position as the camera, OR you save the starting position of the camera and keep using that y instead


works.png (69.3 kB)
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 nickkorage · Nov 26, 2016 at 07:05 AM 0
Share

Yeah, it works as long as the camera goes down. Problem arises when the player stops moving or goes up(Camera is following player) How would you suggest I go about spawning game objects while retaining the gap between the game objects bearing in $$anonymous$$d that the player/camera might stop moving or move up?

avatar image hexagonius nickkorage · Nov 27, 2016 at 12:14 AM 0
Share

I would save the cameras start position at start and use it throughout the game ins$$anonymous$$d of the changing camera position

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning multiple objects in same area without intersecting each other 0 Answers

how to stop Instantiate after 3 minuts,trying to stop Instantiate after 3 minuts 2 Answers

Translate a 2D position to a 3D space 1 Answer

instantiated GameObject Prefab jumps in air after instantiate 1 Answer

How can I naming each instantiated object in "for" loop ? 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