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 Digital-Phantom · Mar 12, 2015 at 09:45 AM · instantiatespawnoriginyaxisxaxis

Instantiated Object spawning at wrong position

I'm trying to make an object/objects instantiate when OnTriggerEnter is activated.

At first this worked fine, the objects appeared when the trigger was entered just as needed.

Original script-

 using UnityEngine;
 using System.Collections;
 
 public class HazzardSpawner : MonoBehaviour
 {
 
     public GameObject hazzardType;
 
     void OnTriggerEnter()
     {
         Instantiate (hazzardType, transform.position, transform.rotation);
         Destroy(gameObject);
 
             }
 }
 

Now I get a little more technical and want to add a variation along the x axis to where they spawn.

new script- using UnityEngine; using System.Collections;

 public class HazzardSpawner : MonoBehaviour
 {
 
     public GameObject hazzardType;
     public int spawnVariation;
 
     void OnTriggerEnter()
     {
         Vector3 spawnPosition = new Vector3 (Random.Range (-spawnVariation, spawnVariation), 0, 0);
         Instantiate (hazzardType, spawnPosition, transform.rotation);
     }
 }
 

It adds the variation along the x axis, but for some reason its also affecting where the objects spawn along the z axis. Every time its spawning the objects about 20 units away from the origin point on the Z axis.

???

alt text

This is the trigger(highlighted) and its origin point.

alt text

I've added this sphere to show where the objects are now spawning from(y axis)

spawn1.png (335.9 kB)
spawn2.png (335.0 kB)
Comment
Add comment · Show 3
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 Stephanides · Mar 12, 2015 at 09:52 AM 0
Share

Im not sure but you always intantiate object on position 0 on Z axis right ?

avatar image Digital-Phantom · Mar 12, 2015 at 09:54 AM 0
Share

With the first script yes, right on 0,0,0 every time.

The second script no, the spawnVariation variable allows me to set a $$anonymous$$/max distance along the X axis where the object can spawn randomly.

The random spawning along the x axis works great, but its always spawning about 20 units away from origin on the z axis (and always the exact same distance)

avatar image Stephanides · Mar 12, 2015 at 10:02 AM 1
Share

You set your Vector3 spawnposition Z axis to 0, then on instantiate you call spawnposition where is Z 0 so it always spawn your object on position 0, try to use no 0 but tranform.positon.z or something. Im not sure if i understood good your problem.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Stephanides · Mar 12, 2015 at 11:18 AM

did you try Vector3 spawnPosition = new Vector3(Random.Range(-spawnVariation, spawnVariation),0 , transform.position.z) ?

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
1

Answer by Yword · Mar 12, 2015 at 10:12 AM

Maybe you should set the spawnPosition relative to the transform's position.

Vector3 spawnPosition = transform.position + new Vector3 (Random.Range (-spawnVariation, spawnVariation), 0, 0);

Comment
Add comment · Show 4 · 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 Digital-Phantom · Mar 12, 2015 at 10:17 AM 0
Share

didn't work and had the weird side effect of constantly spawning objects (ended up with about 500 clones...lol)

:(

avatar image Stephanides · Mar 12, 2015 at 10:33 AM 1
Share

did you try Vector3 spawnPosition = new Vector3(Random.Range(-spawnVariation, spawnVariation),0 , transform.position.z) ?

avatar image Digital-Phantom · Mar 12, 2015 at 10:41 AM 1
Share

Oops.. must have skipped over your comment, never noticed it.

Yes that fixed it. Want to edit your reply and put it as an answer, I'd be happy to up-vote it for you.

Thanks

:)

avatar image Stephanides · Mar 12, 2015 at 11:20 AM 0
Share

Np, im happy that i helped you.

avatar image
0

Answer by Prosun · Jun 26, 2016 at 09:29 AM

i have the same type of issue :(

this is the link of the video which i'm facing right now...

https://www.youtube.com/watch?v=DxROCSN74Uc

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

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

25 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

Related Questions

Instantiate GameObject cloned into wrong position. 1 Answer

Instantiated Object Spawning in different position 2 Answers

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

instanstiate Spawns more then one objec 0 Answers

2d spawning problem 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