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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by kaiyum · Oct 26, 2013 at 04:48 PM · guiworldspacescreenspacetrails

screenspace to worldspace, not working

I am trying to make a mouse trail. When you move your mouse, the gameobject with trail renderer will move along with your mouse. So I first got mouse position which is on screenspace. Then I converted it to worldspace. Then I put the converted position onto trail's position. But it is not working. When I place my mouse on bottom-left corner of screen, only then the trail comes.But it comes at the middle of screen! And if I move mouse by very few pixels, the trail move by large pixels(often out of screen!) The code I am using is below:

 using UnityEngine;
 using System.Collections;
 
 public class debugGUIscript : MonoBehaviour
 {
 public Transform trail;
 public Camera camera;
   void Update()
     {
      Matrix4x4 conv=camera.cameraToWorldMatrix;
      Vector3 convMousePoint= conv.MultiplyPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,0.0f));
 trail.transform.position= new Vector3 (convMousePoint.x,convMousePoint.y, 0.0f); 
     }
 }

What is the problem of my script?

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 meat5000 ♦ · Oct 26, 2013 at 05:04 PM 0
Share

Check your screen space coordinates. They may be different to what you think.

I don't see the screen to world function anywhere. If you do use it, it requires a z depth parameter.

Also, have you tried rendering mouse trail in viewport ins$$anonymous$$d?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by kaiyum · Oct 26, 2013 at 05:53 PM

screen to world method, "Camera.ScreenToWorldPoint". You are referring this,right? Oh boy, thank you very very much. Now I have got it right. For anyone facing similar situation, The changed code for mouse trail is:

  using UnityEngine;
         using System.Collections;
          
         public class debugGUIscript : MonoBehaviour
         {
         public Transform trail;
         public Camera camera;
         void Update()
         {
     Vector3 convMSpoint= camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
     trail.transform.position= new Vector3(convMSpoint.x,convMSpoint.y,convMSpoint.z);
         }
         }

There should be a gameObject with which a trail renderer attached. At first,trail might go to a very biger size. So, we should carefully put nice value on "time" "start width" "end width" "min vertex distance".

I guess we can transform any gameobject into screen by this method,isn't it? Using "Camera.ScreenToWorldPoint" and "Camera.WorldToScreenPoint" with a second depth enabled camera could lead to a nice GUI system with minimal drawcall.

edit I have successfully converted it for touch too. Here is the code for touch devices.

 using UnityEngine;
 using System.Collections;
 
 public class debugGUIscript : MonoBehaviour
 {
     public Transform trail;
     public Camera camera;
     
     void Update()
     {
         foreach (Touch touch in Input.touches) 
         {
         Vector3 convTCpoint= camera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, camera.nearClipPlane));
         trail.transform.position= new Vector3(convTCpoint.x, convTCpoint.y,convTCpoint.z);
                 }
         }
 }
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 meat5000 ♦ · Oct 26, 2013 at 06:05 PM 0
Share

Well, I mentioned Viewport as that is where to GUI lives :) Screen to world has the same effect if you use the Camera near clip plane as the Depth z parameter.

Good job working it out with little to go on :) Accept your answer :)

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

16 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

Related Questions

How to handle UI/Space mixed scene? 2 Answers

GUI Canvas Screen Mode Buttons don't work. 0 Answers

Scalewithscreensize equivalent for worldspace canvas's 0 Answers

Conversion from world to screen space not right? 1 Answer

World space buttons and preventing UI touch passthrough 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