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 /
  • Help Room /
avatar image
0
Question by Walter_Hulsebos · Nov 23, 2016 at 12:51 PM · cameraclickmousepositionsimple

Input.mousePosition to World converts to Camera's position, No matter where I click

alt text

I want the script to draw a line between two positions, the position of the object the script is on and the position of where I clicked the mouse, without Raycasting

 Vector3 spawnPos = transform.position;
 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Debug.DrawLine(spawnPos, mousePos, Color.black);
 
 Draws the line in the picture above.


inputmousepositionv2.png (183.8 kB)
inputmousepositionv2.png (183.8 kB)
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 Walter_Hulsebos · Nov 23, 2016 at 12:55 PM 0
Share

Outdated Code

IEnumerator SimulateProjectile() { Vector3 spawnPos = new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z); //or basically Transform.position Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)); //or Camera.main.ScreenToWorldPoint(Input.mousePostion); Debug.DrawLine(spawnPos, mousePos, Color.black); //draws line between the two positions yield return null; }

avatar image Cherno Walter_Hulsebos · Nov 23, 2016 at 01:22 PM 1
Share

Look at the Scripting API. It statesd that the z value of the Vector corresponds to the distance from teh camera. So, it's no wonder why with a z valuie of 0, the returned Vector3 is equal to the camera's position. I think you might want to use Raycasting ins$$anonymous$$d, however, to get a the position on the surface where your cursor is pointing at.

avatar image Walter_Hulsebos Cherno · Nov 23, 2016 at 01:27 PM 0
Share

No it's not, the same thing happens when you do Camera.main.ScreenToWorldPoint(Input.mousePosition) ins$$anonymous$$d of it.

Plus I'm not planning to use it for getting a position on the surface, I simply added it in this scene for a sense of space.

Show more comments

1 Reply

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

Answer by Mister-Mortal · Nov 23, 2016 at 01:40 PM

You are using Camera.main.ScreenToWorldPoint() wrong. You need to calculate correct z value for the Vector3 parameter. Because you are passing 0f for z it calculates world position 0f units away from camera, which, because of math reasons, always happens to be position of camera.

It's pretty easy to calculate correct z value, here's the example:

 Camera.main.ScreenToWorldPoint(new Vector3(
     Input.mousePosition.x,
     Input.mousePosition.y,
     -Camera.main.transform.position.z + zPositionOfThePlane));
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 Walter_Hulsebos · Nov 23, 2016 at 01:57 PM 0
Share

I rephrased my question, my apologies I wasn't clear when I asked.

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

80 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

Related Questions

Issue with getting the mouse position in 3D and moving a gameobject on the y & z axis 0 Answers

How do I draw a raycast with my mouse position that is relative to the main camera? 0 Answers

Input.mousePosition vector3 in a Camera.ScreenToWorldPoint method not working properly when camera hits its boundary, unity2D 0 Answers

Help with middle mouse panning / dragging 0 Answers

ScreenPointToRay generate wrong ray because camera have a parent with transform 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