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 itsmealex100 · May 12, 2015 at 12:07 PM · c#instantiatescreentoworldpoint

Instantiate Game object at clicked position?

So I am attempting to spawn my particle effect at the clicked mouse position using the code below:

 void OnMouseDown () {
         var    screenPos = Input.mousePosition;
         var worldPos = cam.GetComponent<Camera>().ScreenToWorldPoint(screenPos);
         if (timing == true) {
 
             Instantiate(clickEffect,new Vector2(worldPos.x,worldPos.y),Quaternion.identity);
             Debug.Log(worldPos);
 
 }
 }

The particles spawn and play okay, but they only every spawn in the middle of the screen, not where I click. The debug prints "(0.0, 1.0, -10.0)" no matter where I click. The z being at -10 is fine, but the x and y never change.

I'm not fully clear on converting screen coordinates to world but from what I've read elsewhere I thought this was right, what have I done wrong?

Any help would be greatly appreciated, thanks

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 xortrox · May 12, 2015 at 12:09 PM 0
Share

This depends a lot on where you actually want to spawn the particles, do you have a plane they spawn on? Just clicking on screen wont tell the game how far into the 3D world it should spawn, this would require a Raycast if the camera has perspective. If it's orthographical I believe this should be working already?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eudaimonium · May 12, 2015 at 01:08 PM

You made "worldPos" variable but never actually use it:

Correction:

   Instantiate(clickEffect, worldPos, 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
avatar image
0

Answer by hbalint1 · May 12, 2015 at 12:20 PM

I don't think OnMouseDown is the right event you want to use. Put your code snippet in the Update Method and use Input.GetMouseButton(0) instead. as the docs says http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html is for clicking GUI elements and Colliders.

 void Update() {
      if(Input.GetMouseButton(0)){
          var screenPos = Input.mousePosition;
          var worldPos = cam.GetComponent<Camera>().ScreenToWorldPoint(screenPos);
          if (timing == true) {
  
              Instantiate(clickEffect,new Vector2(screenPos.x,screenPos.y),Quaternion.identity);
              Debug.Log(worldPos);
          }
      }
  }

I don't understand why did you calculate the worldPos if you don't use it anyway.

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 xortrox · May 12, 2015 at 12:26 PM 0
Share

He might be clicking a plane, but yeah he didn't use worldPos.

avatar image itsmealex100 · May 12, 2015 at 12:36 PM 0
Share

Sorry my mistake, I wasn't using the worldPos, I have edited. However even when i do use it, the particles still only spawn in middle of the screen. And yes I was using a plane with a box collider, It seemed pointless to constantly check if mouse button was down

avatar image hbalint1 · May 12, 2015 at 01:04 PM 0
Share

oh. i thought you can click anywhere (above or below the plane), sorry. Are you sure "cam" is the camera are you using?

avatar image itsmealex100 · May 12, 2015 at 04:15 PM 0
Share

Yep, i've double checked that and it is.

avatar image
0
Wiki

Answer by xortrox · May 12, 2015 at 01:41 PM

After you clearified that you did in fact use a plane with a box collider, the solution is easier if you use a raycast:

 void OnMouseDown () 
 {
 var    screenPos = Input.mousePosition;
 var ray = cam.GetComponent<Camera>().ScreenPointToRay(screenPos);
 if (timing == true) 
 {

 RaycastHit hit;
 if(Physics.Raycast(ray, out hit, Mathf.infinity))
 {

 if(hit.collider.name ="NameOfPlane")
 {

 Instantiate(clickEffect,new Vector2(hit.point.x, hit.point.y), Quaternion.identity);
 Debug.Log(ray);

 }
 else
 {

 Debug.Log("Plane was clicked but raycast hit something else.");
 
 }

 }
 else
 {
 
 Debug.Log("Plane was clicked but raycast missed.");
 
 }
 
  
 }

 }

Also the ideal solution to this might be to use OnMouseUp for expected behaviour by your users (It's not normal to have a click happen before you release the button in most cases).

Also sorry for the poor indentations, not sure how to "tab" in code blocks on here yet.

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 itsmealex100 · May 12, 2015 at 04:15 PM 0
Share

Unfortunately this did not seem to work, there was no errors but nothing happened upon click. However I should have mentioned that Raycast seems unnecessary because this script is attached to the Plane with the box collider which the player clicks. Basically this object gets instantiated where ever the player clicks on the screen. The camera does not ever move either.

avatar image xortrox · May 12, 2015 at 04:22 PM 0
Share

$$anonymous$$ake sure ti$$anonymous$$g is true and that the cam variable is even assigned, even if the camera never moves you will have to translate the click to 3D coordinates if the camera has perspective on it. The easiest way to do that would be to use a raycast.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Problem with ScreenToWorldPoint and touches (C#) 2 Answers

Distribute terrain in zones 3 Answers

Instaniate Prefab 1 Answer

C# 2d Instantiate object randomly around gameobject 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