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
2
Question by Ashkan_gc · Apr 13, 2010 at 06:21 AM · camerainputmouseraycasting

how can i instantiate a GameObject in mouse position and in a special distance from the camera?

i want to instantiate a GameObject in mouse position and i can use camera.ScreenToWorldPoint or ScreenPointToRay methods for finding the world position. i can not use raycasting in my situation. so i need a method to find what is the world position of mouse in (for example 100m away) from the camera position. because camera view is perspective and the view is a cone some calculations are needed. as i know i should take the difference of mouse position from center, into account and use the fieldOfView value to find the angle of the line that i should go on to find the position but i can not produce working code. my code is like this inside Update() function.

 if (Input.GetMouseButtonDown(0))
        {
            Vector3 a = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,Input.mousePosition.y,0));
            a.z = 0;
            print(a.z.ToString());
            Instantiate (fairy,a,Quaternion.identity);
        }

the camera is placed at (0,0,-10) and i want to create fairies at z position of 0.

i have a related question as a featured question too.

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 alexnode · Apr 13, 2010 at 08:35 AM 0
Share

If the mouse was always at the center of the screen it would have been easy.You find a vector of where you are looking at + 100m depth from your current position. But In your case it is a bit of a pain for small brains like $$anonymous$$e . In purely geometrical terms you need to find a vector from your camera to the mouse and then on the vector axis to go 100m in depth. And Then you have to eli$$anonymous$$ate the bloody perspective !!! but do you need to eli$$anonymous$$ate perspective ? The horizontal plane from the center of the camera is not distorted.or not ??? Challenging ... If I was a bit more knowledgeable...

2 Replies

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

Answer by duck · Apr 13, 2010 at 08:45 AM

Use Camera.ScreenToWorldPoint.

To use it, you provide it with a Vector3. The X & Y components of the Vector3 represent the pixel coordinates, measured from the bottom-left of the screen. The Z component represents the distance from the camera in world-units.

For example:

// instantiate an object at the mouse's position, at 20 units away from the camera
var screenPos = Input.MousePosition;
screenPos.z = 20;
var worldPos = camera.ScreenToWorldPoint(screenPos);
var newInstance = Instantiate(prefab, worldPos, Quaternion.identity);
Comment
Add comment · Show 7 · 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 Ashkan_gc · Apr 13, 2010 at 02:08 PM 0
Share

unfortunately it don't work. the position that ScreenToWorld point gets to me is at most my current x/y -1 or +1 and in 20 meters away it's much near the center and the point that mouse is pointing to is my x + 5. can you get the point. we need to rotate the line between ScreenToWorld's result and z+20. i think it should be rotated dipending on mouse pos and fieldofview.

avatar image duck ♦♦ · Apr 13, 2010 at 03:29 PM 0
Share

It does work. I just tried it, and objects appear directly under my mouse cursor, at the specified distance away in the scene. Unity does the projection calculations for you, so that the direction radiates away from the camera's position at exactly the right angle so that the object is under the mouse at whatever distance you specify. There must be some other problem in your code, or in the way that your scene is set up. Check that you're converting from the correct camera (if you have more than one cam in your scene), and perhaps you could post your code if you're still having problems.

avatar image Ashkan_gc · Apr 14, 2010 at 01:17 PM 0
Share

i edited my answer to include the code. the code is like yours but the result is not. the camera is placed at z pos of -10.

avatar image duck ♦♦ · Apr 14, 2010 at 01:40 PM 0
Share

In your code, you're specifying a Z value of zero. This means the object will be created at exactly the same position as the camera (zero units away). Is that what you want? In my example, it positions the objects 20 units away from the camera. I'm having trouble understanding what you want - perhaps the best way to explain it might be to give any examples you can find of any other games which show the behaviour that you want?

avatar image Ashkan_gc · Apr 14, 2010 at 05:08 PM 0
Share

my camera's z is -10 so 0 is 10 units away from the camera.

Show more comments
avatar image
0

Answer by Ashkan_gc · Apr 14, 2010 at 05:36 PM

duck's answer is correct. thank you man! i write this for clearification. i read the docs carelessly. it says that the third argument of ScreenToWorldPoint method should be the z's distance from camera and not in world/scene space. so when my camera is at z of -10, 0 means 10 meters away from the camera and code be like this.

if (Input.GetMouseButtonDown(0))
        {
            Vector3 a = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,Input.mousePosition.y,10));
            Instantiate (fairy,a,Quaternion.identity);
        }
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

No one has followed this question yet.

Related Questions

Ray Over Mouse 1 Answer

using left right keys to turn 1 Answer

Move camera when right mouse button is pressed 3 Answers

Need help on the 3ds max style camera control 0 Answers

Simple Object Dragging 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