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 SecretAgentMango · Jul 31, 2021 at 04:42 PM · instantiatetransform.position

Object will not instantiate properly as a Vector2 stored in a variable, but will when a new Vector2 is created?

I am trying to create a feature where you can drag and drop an icon from your inventory and it will "drop" the item on the floor in the game. This is the function I created for doing that:

     public void DropItemOnGround()
     {
         // Drop the item on the ground (with an offset)
         List<int> posOrNeg = new List<int>() { -1, 1 };
         float offsetX = Random.Range(0, 0.4f) * posOrNeg[Random.Range(0, posOrNeg.Count)];
         float offsetY = Random.Range(0, 0.4f) * posOrNeg[Random.Range(0, posOrNeg.Count)];
         Vector2 dropPosition = new Vector2(transform.position.x + offsetX, 
             transform.position.y + offsetY);
 
         Instantiate(gameController.GetComponent<DropController>().GetPickup("Sword"), 
             dropPosition, transform.rotation);
     }

This item is attached to my player, and the "dropPosition" basically creates a little bit of variation in where the item is dropped relative to the player so the items don't all stack on each other.

For whatever reason, the item does not drop on the ground and just re-instatiates itself in the menu as an icon. However, if I change my code so that instead of "dropPosition" I just manually create a new Vector2:

     public void DropItemOnGround()
     {
         // Drop the item on the ground (with an offset)
         List<int> posOrNeg = new List<int>() { -1, 1 };
         float offsetX = Random.Range(0, 0.4f) * posOrNeg[Random.Range(0, posOrNeg.Count)];
         float offsetY = Random.Range(0, 0.4f) * posOrNeg[Random.Range(0, posOrNeg.Count)];
         Vector2 dropPosition = new Vector2(transform.position.x + offsetX, 
             transform.position.y + offsetY);
 
         Instantiate(gameController.GetComponent<DropController>().GetPickup("Sword"), 
             new Vector2(-1.8f, 2.0f), transform.rotation);
     }

Now the item drops on the ground as I expected. Obviously, I cannot dynamically change the spot the weapon is dropped without a variable. I don't understand why this is happening, I've also tested printing out exactly what "dropPosition" equals and it does come out as a legitimate, expected Vector2 with expected coordinates. Any ideas? Thank you!

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 Bunny83 · Jul 31, 2021 at 06:01 PM

For whatever reason, the item does not drop on the ground and just re-instatiates itself in the menu as an icon.

That sounds like you may have some pick up functionality in your game? So you probably instantiate the object within the radius of the player so it picks it up in the moment you dropped it. Though we can not really tell since we don't know literally anything about your game or what game mechanics you have.


Usually you want to drop items "before" the player and not in or around the player. Though if you have a pick up script it's not clear what range this pickup script has. Most games do implement a cooldown on the items which prevent the item from being picked up immediately for exactly this reason.

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

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

Related Questions

Instantiate object at specific distance from transform position? 1 Answer

How to count instantiated objects that passes a given point (transform.position.y) 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Calculating where to position instantiated clone 1 Answer

Checking if object intersects? 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