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 AeonIxion · Oct 29, 2012 at 10:08 AM · camerainstantiatemouseposition

Instantiate at mouseposition problem

Hello all,

I'm trying to instantiate an object at my mouseposition when I type 'i'.

Here's my code:

 if(Input.GetKeyDown("i"))
         {
             print("input: " + Input.mousePosition);
             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             //pos.y = 0;
             print("pos: " + pos);
             Debug.Break();
             cObject = (GameObject) GameObject.Instantiate(prefab, pos, Quaternion.identity);
         }


However, when I type 'i' in the gameview, it instantiates the object at the Cameraposition, not at the mouseposition. I've used Camera.ScreenToWorldPoint and Instantiate before, but not together. Is there something I'm missing?

Comment
Add comment · Show 4
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 FlyingOstriche · Oct 29, 2012 at 10:14 AM 0
Share

Try setting the a z value for screenToWorldPoint. Is this supposed to be 2D or 3D? what ouputs do the print statements give?

avatar image AeonIxion · Oct 29, 2012 at 10:19 AM 0
Share

When I set a z value of 20, it instantiates the object at x, y of camera position and z of 20.

It is 3D, but I want the y of the objects to be 0.

avatar image FlyingOstriche · Oct 29, 2012 at 10:27 AM 0
Share

what does it output for "input:" and "pos:"

avatar image sparkzbarca · Oct 29, 2012 at 10:46 PM 0
Share

a y of zero can be set by making it spawn at zero, are you looking down on this?

if thats so then the mouse "Y" is actually your Z axis locally.

3 Replies

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

Answer by fafase · Oct 29, 2012 at 10:20 AM

Your issue is simple, you need to cast a ray using raycast from your pos

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 if (Physics.Raycast (ray, hit)) {
     Debug.DrawLine (ray.origin, hit.point);
 }
 GameObject obj = (GameObject)Instantiate(prefab,hit.point,Quaternion.identity);

Problem was you were converting from the screen to the real world of the game but still on the camera screen. You need to throw a laser beam from that point to the world and where it hits is what you are pointing with the mouse.

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 AeonIxion · Oct 30, 2012 at 07:28 AM 0
Share

I thought about it in the beginning, but decided I didn't have to use it. Then I just forgot about it.

Everyone else was right, but setting a z-axis meant it would always spawn an object at the x/y of the camera, regardless of my mouseposition.

This is what I was looking for, though! thanks all.

avatar image
0

Answer by sparkzbarca · Oct 29, 2012 at 10:16 AM

Well you told it to do so.

Basically your problem is your trying to use 2d coordinates and get a 3d point.

Screen to world point from mouse will work but the mouse is basically at the cameras position.

What you need to do it give the object a predefined depth, that is what your missing, your spawning it in the cameras plane just slightly moved from it.

do something like

 depth = 3;
 
 pos = new vector3(input.mouse.x, input.mouse.y, depth);
 

Start with 3 for the depth, thats a good easy to see it now distance. Then change the depth to suit your needs, you can even assign the mouse scroll wheel to modify the depth value. Best of luck! Mark as answered if so.

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
0

Answer by aldonaletto · Oct 29, 2012 at 10:27 AM

@FlyingOstriche is right: a single point in the 2D screen corresponds in the 3D world to an infinite line passing through the camera and the mouse pointer, thus you must specify which point of this line you want. ScreenToWorldPoint selects this point based on the Z coordinate: it must be the distance from the camera - if you pass Input.mousePosition, z is zero and the point returned is exactly the camera position.
You can pass a constant in Z like this:

   ...
   Vector3 mPos = Input.mousePosition;
   mPos.z = 20.0f; // select point at 20 units
   Vector3 pos = Camera.main.ScreenToWorldPoint(mPos);
   ...
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

13 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

Related Questions

Camera ScreenToWorldPoint Instatiating 1 Answer

Camera.ScreenToWorldPoint with Perspective camera 1 Answer

C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer

Instantiate a button at mouse click position, 0 Answers

Mouse position in camera view area 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