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 Squallara · Sep 16, 2015 at 06:50 PM · gameobjectraycastprefabsmouse positionlevel editor

Create a prefab object into the scene exactly where the mouse is pointing

Hello Guys,I am new to Unity and I would like your help if it is possible. I made a new window like a level editor and I made a button there called "cube". I have a prefab cube and I would like when I click the button "Cube" to go into the scene and start drop prefab cubes exactly where my mouse is pointing. I tried a lot before such as rays and Event.mousePosition but I couldn't make it to work. I guess that is has to be done with a Ray but as I told you I am new and I make something wrong. I will appreciate any help.

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
1
Best Answer

Answer by MakeCodeNow · Sep 17, 2015 at 03:04 AM

You've got the right idea. You need to use the Event.mousePosition, use Camera.ScreenPointToRay to convert that into a Ray, and then intersect that Ray with the world. The question is, how do you intersect the ray with the world. If you want to check against collision, use Physics.Raycast. However, it's possible that this returns no results (like in an empty scene). In that case, you probably want to place the object where the ray intersects the XY plane. To do that, you need to create a Plane object and use it's Ray intersection method to compute where the ray and plane intersect.

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 Squallara · Sep 17, 2015 at 07:14 AM

Thank you for your Answer. This is my code of my window:

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 public class LevelEditor : EditorWindow
 {
 
     // Add menu item named "My Window" to the Window menu
     [MenuItem("Window/Level Editor")]
     public static void ShowWindow()
     {
         //Show existing window instance. If one doesn't exist, make one.
         EditorWindow.GetWindow(typeof(LevelEditor));
     }
     
     //It is a method like for display things in our Editor
     void OnGUI()
     {
         //GUILayout.Button("...") creates a button named ... to our Window Editor
         if(GUILayout.Button("Cube"))
         {
             CreateCubes.OnSceneGUI();
         }
     }
 }    

and this is the code I try to cast the ray:

 using UnityEngine;
 using System.Collections;
 using System;
 using UnityEditor;
 
 public class CreateCubes : Editor 
 {
 
     public static void OnSceneGUI()
     {
         Debug.Log("You clicked Button");
         if (Event.current.type == EventType.MouseDown)
         {
         Ray ray = Camera.main.ScreenPointToRay(Event.current.mousePosition);
         RaycastHit hit = new RaycastHit();
         if (Physics.Raycast(ray, out hit, 1000.0f))
         {
         Debug.Log(Event.current.mousePosition);
         Vector3 newTilePosition = hit.point;
         Instantiate((GameObject)Resources.Load("prefabs/WholeCube", typeof(GameObject)), newTilePosition, Quaternion.identity);
         }
         else 
         {
         Debug.Log ("Problem with the Physics");
         }
         }
         else {Debug.Log ("Ray didn't get value");}
     }
 }

I get the log "You clicked Button" and "Ray didn't get value" Why?

Comment
Add comment · Show 4 · 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 MakeCodeNow · Sep 18, 2015 at 03:36 AM 0
Share

Is your code here correct? the way it's written, "You clicked button" will always happen and "Ray didn't get value" happens anytime Event.current.type is not equal to EventType.$$anonymous$$ouseDown.

Basically, your logging doesn't match the logic.

avatar image MakeCodeNow · Sep 18, 2015 at 03:37 AM 0
Share

Also, if your scene is empty, Raycast will always fail. See my original answer regarding doing by physics raycasts and Plane intersections.

avatar image Squallara · Sep 18, 2015 at 07:51 AM 0
Share

Yes I found out the most of them. These are my codes now:

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 
 public class LevelEditor : EditorWindow
 {
     bool GUI$$anonymous$$eyDown($$anonymous$$eyCode key)
     {
         if (Event.current.type == EventType.$$anonymous$$eyDown)
             return (Event.current.keyCode == key);
         return false;
     }
 
     // Add menu item named "$$anonymous$$y Window" to the Window menu
     [$$anonymous$$enuItem("Window/Level Editor")]
     public static void ShowWindow()
     {
         //Show existing window instance. If one doesn't exist, make one.
         EditorWindow.GetWindow(typeof(LevelEditor));
     }
 
     void OnGUI()
     {
         GUILayout.Button("Cube");
         if (GUI$$anonymous$$eyDown($$anonymous$$eyCode.C))
         {
             CreateCubes.OnSceneGUI();
         }
     }
 }    

I made a shortcut for placing the cubes with "C" button.

The code for creating them is:

 using UnityEngine;
 using System.Collections;
 using System;
 using UnityEditor;
 
 public class CreateCubes : Editor 
 {
     
     public static void OnSceneGUI()
     {
         Debug.Log("You clicked Button");
         if (Event.current.type == EventType.$$anonymous$$eyDown)
         {
             Ray ray = Camera.main.ScreenPointToRay(Event.current.mousePosition);
             Debug.Log(ray);
             RaycastHit hit = new RaycastHit();
             Debug.Log(Physics.Raycast(ray, out hit, 1000.0f));
             Debug.DrawRay(ray.origin, ray.direction, Color.red, 3);
             if (Physics.Raycast(ray, out hit, 1000.0f))
             {
                 Debug.Log(Event.current.mousePosition);
                 Vector3 newTilePosition = hit.point;
                 Instantiate((GameObject)Resources.Load("prefabs/WholeCube", typeof(GameObject)), newTilePosition, Quaternion.identity);
             }
             else 
             {
                 Debug.Log ("Problem with the Physics");
             }
         }
         else {Debug.Log ("Ray didn't get value");}
     }
 }

I changed the condition "(Event.current.type == EventType.$$anonymous$$eyDown)".

I forgot to mention that I have a plane already in my scene. Now it seems to working but my problem is that the cubes aren't placing exactly at my mouse position. Do you know why? $$anonymous$$aybe if I change the camera.main to camera.current? Thank you once again for you time.

avatar image MakeCodeNow · Sep 21, 2015 at 02:47 AM 0
Share

I think your original question has been answered. Now you just need to do more debugging. Please mark the question as answered and go to the forums if you want more help with debugging your code.

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

31 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

Related Questions

Offset between mouse cursor position and "hit" position with worldspace UI's 1 Answer

ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer

Raycast doesn't find mesh 0 Answers

RayCasting from a Empty GameObject 1 Answer

[SOLVED] get object which is nearby to mouse 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