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 jmfp92 · Jun 19, 2020 at 09:00 AM · 2dinstantiatemouseposition

Instantiate gameobject at mouseposition 2d

I know this question has been answered many times, but no matter what I do, the object keeps instantiating at the middle of the screen, but slightly towards where my mouse is. Here's what I've tried.

 Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.nearClipPlane));
 
             Instantiate(go, worldPoint, Quaternion.identity);

I have also tried using the ScreenPointToRay and storing the origin in a vector3

 Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.nearClipPlane));
 Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.nearClipPlane));
 
             Instantiate(go, new Vector3(ray.origin.x, ray.origin.y, 1f), Quaternion.identity);

I've also used the basic Input.mousePosition and pretty much every other answer, any idea or am I just stupid and missing something simple?

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 jmfp92 · Jun 19, 2020 at 09:01 AM 0
Share

my near clip plane is 0.3 and my camera's z position is -11

2 Replies

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

Answer by jmfp92 · Jun 23, 2020 at 01:37 AM

Thanks to @tadadosi, I was able to get a script that should work for whatever setup you have. As they said you have to add depth to the mouse position to get the true worldposition, but it shouldn't be any arbitrary number or else it may not work as needed. Instead, use the absolute of your camera's z position

 using UnityEngine;
 
 public class Builder : MonoBehaviour
 {
     public GameObject go;
     Camera cam;
 
 
     public void Awake()
     {
         cam = Camera.main;
     }
     
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Vector3 worldPoint = Input.mousePosition;
             worldPoint.z = Mathf.Abs(cam.transform.position.z);
             //worldPoint.z = 11f;
             Vector3 mouseWorldPosition = cam.ScreenToWorldPoint(worldPoint);
             mouseWorldPosition.z = 0f;
             Instantiate(go, mouseWorldPosition, 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
1

Answer by tadadosi · Jun 19, 2020 at 12:55 PM

This is the best way I've found so far to transform from mouse screen pixel point to world point, the key here is to add depth to the pixel coordinates and then remove that depth after transforming to world coordinates.

Add this class to a gameobject, assign a prefab, hit play and (if you got an orthographic camera setup) you should be able to instantiate that prefab on the position where you pressed your left mouse button.

 using UnityEngine;
 
 public class InstantiateOnMousePosition : MonoBehaviour
 {
     public GameObject prefabToInstantiate;
 
     private Camera _Camera;
 
     private void Awake()
     {
         // Store your camera on Awake (better performance).
         _Camera = Camera.main;
     }
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             // Store current mouse position in pixel coordinates.
             Vector3 mousePixelPos = Input.mousePosition;
 
             // Add depth so it can actually be used to cast a ray.
             mousePixelPos.z = 20f;
 
             // Transform from pixel to world coordinates
             Vector3 mouseWorldPosition = _Camera.ScreenToWorldPoint(mousePixelPos);
 
             // Remove depth
             mouseWorldPosition.z = 0f;
 
             // Spawn your prefab
             Instantiate(prefabToInstantiate, mouseWorldPosition, 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 tadadosi · Jun 19, 2020 at 08:05 PM 0
Share

@jmfp92 Where you able to find the answer to your question?

avatar image jmfp92 · Jun 23, 2020 at 01:19 AM 0
Share

Thank you! that PRETTY $$anonymous$$UCH worked for me except I had to change change the mousePixelPos.z to be 10 not 20 to get the object to instantiate close to where I am clicking on the screen and I noticed the further along in any direction on the x or y axis I go from zero to instantiate it's less reliable (object instantiates further away from mouse position) I'm assu$$anonymous$$g there is probably some property of the camera that is a float value we can use to make this more reliable, any input on that?

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

296 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 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

How to get bullet to fire at set velocity? [2D] 1 Answer

Instantiate at mouse in empty space 2D 0 Answers

Create objects on a mouse click in 2D 1 Answer

How in the world do I ACCURATELY place an object at the mouse? 3 Answers

How do I Instantiate a prefab in a randomly generated location specific to tile type (2D Procedural game) 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