Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by BarkShark · May 21, 2011 at 10:32 AM · instantiatemouse position

instantiate object at mouse position

Hello

Could anyone please explain me how i have to instantiate a prefab at the current mouse position? I already know how to instantiate prefabs but what i don't know is how to instantiate them at the currrent mouse position.

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

4 Replies

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

Answer by Edy · May 21, 2011 at 10:56 AM

Get the current mouse position with Input.mousePosition, then convert that position to world space using Camera.ScreenToWorldPoint.

EDIT: Example in C#:

 void Update () 
     {
     if (Input.GetButtonDown("Fire1")) 
         {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 2.0f;       // we want 2m away from the camera position

         Vector3 objectPos = Camera.current.ScreenToWorldPoint(mousePos);
         Instantiate(yourObject, objectPos, Quaternion.identity);
         }
     }

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 BarkShark · May 21, 2011 at 11:01 AM 0
Share

How do i have to do that? I've never worked with Input.mousePosition before? Could you please explain it a little more in detail?

avatar image Edy · May 21, 2011 at 11:15 AM 0
Share

I've edited the answer to include an example code.

avatar image BarkShark · May 21, 2011 at 11:42 AM 0
Share

I have keep getting an error. I posted the error as an answer because i couldn't attach my script in a comment :D

avatar image stjernerlever · Dec 23, 2019 at 11:58 PM 1
Share

If I use the above I get an error. If I change Camera.current to Camera.main, my prefab will spawn. However, my player is a god, watching the world from above (3D game), and the prefab is spawned at the gods position. How can I get the prefab to spawn down on the ground?

avatar image
1

Answer by BarkShark · May 21, 2011 at 11:41 AM

I keep getting a error when i press the mouse:

NullReferenceException UnityEngine.Camera.ScreenToWorldPoint (Vector3 position) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/Graphics.cs:650)

Do you have any idea what this is?

This is my sript:

 var testObject : Transform;
 
 function Update () 
     {
     if (Input.GetButtonDown ("Fire1")) 
         {
         var mousePos = Input.mousePosition;
         mousePos.z = 2.0;       // we want 2m away from the camera position
 
         var objectPos = Camera.current.ScreenToWorldPoint(mousePos);
         
         var myObject= Instantiate(testObject, objectPos, Quaternion.identity);
         }
     }
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 Edy · May 21, 2011 at 02:27 PM 1
Share

Try using Camera.main ins$$anonymous$$d of Camera.current. If the error persists, include a reference to your main camera in the script.

avatar image leonalchemist · May 21, 2011 at 02:31 PM 0
Share

im not too great at unity but i had to do something similar for a 2D game, shouldnt u maybe create the variable objectPos as a camera object by putting var objectPos : Camera, before the Update function and set it in unity to ur main camera.

avatar image
1

Answer by Maverickthebest · Jan 06, 2019 at 06:02 AM

mousePos.z = 2.0; is this not float?? i mean: mousePos.z = 2.0f;

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 Edy · Jan 06, 2019 at 11:15 AM 0
Share

The code is unityscript, not C#. Floats don't use the -f suffix here.

avatar image JackhammerGaming Edy · Apr 19, 2020 at 02:38 PM 0
Share

yes sir it does matter and it is c# and they use -f suffix

avatar image
1

Answer by SystemSuitable · Apr 13, 2020 at 10:16 PM

For anyone attempting to do this in C# is quite simple & works quite fine as of 4/13/20...

 private Vector3 mousePos;
 private Vector3 objectPos;
 public GameObject yourPrefab;

 void Update ()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         mousePos = Input.mousePosition;
         mousePos.z = 2.0f;
         objectPos = Camera.main.ScreenToWorldPoint(mousePos);
         Instantiate(yourPrefab, objectPos, Quaternion.identity);
     }
 }

}

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 unity_lIp-DTlGzNpjJA · Apr 19, 2020 at 02:04 PM 0
Share

i have something simmilar but i cant get it to appear on the game screen only on the scene screen can you help me

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

8 People are following this question.

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

Related Questions

Can't get Instantiated Object to Follow Mouse Position(Solved) 2 Answers

mouse position on tag 1 Answer

Checking if object intersects? 1 Answer

how can you make an instantiated game object appear in the position where the mouse was clicked? 0 Answers

Unity mouse input gradually becomes off 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