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 ktmarine1999 · May 24, 2015 at 07:49 PM · mousescene view

How to get the 3d mouse position in the editor window

I know that I can use Event.current.mousePosition to get the mouse position, but the problem is it only returns a Vector2, and I need a Vector3 to cast a ray. I am trying to place objects in a x,z grid in the scene view.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by 3ddave · May 26, 2015 at 04:02 AM

Unity has built in functions for casting a ray into the scene based on a screen point. See the example code on Input.mousePosition.

You can get more information on the hit by providing a RaycastHit object to the function call like so

 var hit : RaycastHit;
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 if(Physics.Raycast(ray, hit)){
    //ray hit object, info stored in hit
    Debug.Log("Hit obj: " + hit.transform.gameObject);
 }else{
    //nothing hit by ray
    Debug.Log("Click missed");
 }


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 ktmarine1999 · May 28, 2015 at 07:02 AM 0
Share

The problem is that input.mousePosition only works in the game view when the game is running. I am trying to do it in the scene view with out the game running. I have a grid laid out on the x,z plane, the current code that I am using works in the x,y plane. It is similar to the code you presented except it uses Event.current.mousePosition ins$$anonymous$$d of input.mousePosition. since these are the only too ways i might have to do my editing in the x,y plane and then rotate it to the x,z plane when the game starts, or maybe try to see if there is a way to make z be up in the physics system, or just completly turn gravity off and use my own gravity code.

avatar image 3ddave · May 30, 2015 at 12:05 AM 0
Share

O$$anonymous$$, I see. It appears that might be a bit trickier. I'm having trouble visualizing exactly what you are trying to do but perhaps the following resources will guide you. OnSceneGUI Scripting - This is an old post, but uses code similar to $$anonymous$$e only calling HandleUtility.GUIPointToWorldRay for the ray casting. It sounds like this person has achieved essentially what you are trying to do (position things in the scene view in 3D). If that doesn't work, you might try your second approach which is to modify the orientation of the scene view. This can be done but seems to utilize undocumented features (so use at your own risk). See Orthographic scene view and $$anonymous$$anipulating the Scene View from script. Hope that helps.

avatar image
0

Answer by ktmarine1999 · May 30, 2015 at 11:31 AM

Basically what I am trying to do is build a 3D map builder for the scene view. I have a bunch of modular pieces, i.e 2 different types of floor tiles, 2 Wall types, a door. I have a grid drawing on the screen that is 20 tiles by 20 tiles, with each tile being the same width and length of my floor tile. I originally used Input.mousePosition, like you suggested but it was not working. After some research found out that Input.mousePosition only works when the game is running I then came across the 2D Tilemap Starter Kit which is close to what I was doing and found that they used Event.current.mousePosition to get the mouse position, my editor was working, sort of, the x, and y worked perfect but my selection box wasn't moving in the z direction, Back to the documentation I went, maybe I wasn't using it correctly. Come to find out it returns a vector2 not a vector3 which is why it worked on the x,y dug through the GUI Scripting guide and was left with no solution, doing an internet search gives you Input.mousePosition, I did stumble across a post where someone was using a different api stack and sending it the window handle and got it working, no details. so it is to the community with this question hoping someone some place might know of a way to to do 3D picking in the s Scene View while not in play mode. I know there are some things that are not in the scripting api reference, i.e I am using UnityEditor.SceneView.currentDrawingSceneView.camera.ScreenPointToRay there is nothing for currentDrawingSceneView but the camera works like a normal camera, when you do a search for SceneView you get results for the MouseCursor and that's it. I was really hoping that there was something similar for getting a vector3 for the mouse position. If there is not then I think I will just rotate everything 90 degrees on the x axis when the game starts. Kinda of a hack but should work, there is all kinds of documentation about rotating the transform of objects when the game is up and running.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why does the scene view camera move too quickly? 0 Answers

Scene Editor: panning with mouse pops when cursor goes offscreen 0 Answers

Known issue with Unity Editor on Surface Pro? 3 Answers

How to transform objects along last-used axis using middle mouse and drag? 0 Answers

Using mouse for realistic Top-Down space ship yaw control 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