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 Madcowe · Nov 29, 2015 at 09:59 AM · spawnspawningspawnpoints

Being spawned inaccurately on level load.

Hey so I've been working on a simple 2D platformer.

To make things simpler I created an empty game object called Spawn Point with a very small script to instantiate the player:

 public GameObject player;
 public GameObject spawnPoint;
 
     void Start ()
     {
         Instantiate(player);
         player.transform.position = spawnPoint.transform.position;
     }

However, whenever the level restarts or a new level loads the character can appear quite a distance away from it. Usually about 2 or 3 "units" which can make the player start on the other side of the wall and not on the intended starting position... Like on top of a hole or on one of the items to collect.

I really have no idea why this could be happening, I even tried settings the position before instantiating (and didn't get any errors for the player not existing yet, funnily enough) but that still didn't work...

After resetting the level or dying (resetting the level therefore) it always instantiates correctly though...

Please let me know if you need more details, I really don't know what could be causing it and I don't want to tell you everything as it might be unrelated xD

Comment
Add comment · Show 1
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 beppim · Nov 29, 2015 at 10:36 AM 1
Share

Check this, maybe obvious: - The transform on the prefab is set to (0,0,0)

Did you try to instantiate giving the position? Instantiate (player, position, rotation);

Did you try to set the player with position Vector3.zero as a child of spawnPoint?

Anyway it seems a fight between local and world positioning, I would debug that.

1 Reply

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

Answer by Pixzle · Nov 29, 2015 at 10:54 AM

You should first check if the prefab position is set to 0,0,0. What your code is doing now is instantiating the player, then modifying the position of the actual prefab, not the instance you just created.

You could either do somethink like:

          player.transform.position = spawnPoint.transform.position;
          Instantiate(player);

this will edit the prefabs position before instantiating it.

You could also do something like:

  public GameObject player;
  GameObject instPlayer;
  public GameObject spawnPoint;
  
      void Start ()
      {
          instPlayer = Instantiate(player) as GameObject;
          instPlayer.transform.position = spawnPoint.transform.position;
      }

but easiest should simply be:

  public GameObject player;
  public GameObject spawnPoint;
  
      void Start ()
      {
          Instantiate(player, spawnPoint.transform.position, spawnPoint.transform.rotation);
      }

Code is untested, but it should work. I hope it helps!

Comment
Add comment · Show 5 · 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 Madcowe · Nov 29, 2015 at 11:31 AM 0
Share

Your latest solution appears to have worked properly :D

Perhaps it was an issue with rotation... since I am working on a 2D game the sprites wouldn't have rotated so it could have cause that...

Either way my prefabs were all set to (0,0,-1) with (0,0,0) rotation so not sure what happened x)

avatar image Pixzle Madcowe · Nov 29, 2015 at 12:45 PM 1
Share

Cool to hear it works. What you did with your code was setting the prefabs position after instantiation, so next time you instantiate they get that offset.

 player.transform.position = spawnPoint.transform.position;

only edits the PREFABS offset :D

avatar image Madcowe Pixzle · Nov 29, 2015 at 02:58 PM 0
Share

oh I think I see what you mean now xD

The prefab is set to (0,0,0), so by instantiating it first it would appear on that position and only after would it change to the spawn position xD

But why then didn't it change position anyway?

Is it because the physics kicked in and changed the current position ins$$anonymous$$d of actually moving it to the spawn point?

Show more comments

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

how to "search" for a spawn point 0 Answers

Prevent object from spawning at a spawn point twice in a row? 3 Answers

[UNET] Dynamically position NetworkStartPosition Gameobjects 2 Answers

Spawn Point 2 Answers

Network spawning 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