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 Paul Graystone · Jan 26, 2013 at 02:44 PM · javascriptmousedragworldtoscreenpoint

Drag Object along local z-axis. Using WorldToScreenPoint.

I want to drag an object along its local z-axis. Inspired by robertbu's answer on one of my last question I tried to implement the dragging without using RayCasts.

This is the code so far:

 private var mainCam : Camera;
 private var v3StartPos : Vector3;
 private var v2Pivot     : Vector2;
 private var v2ScreenStartPos : Vector2;
  
 function OnMouseDown(){
     mainCam = GameObject.FindWithTag("MainCamera").camera;
     v3StartPos = transform.localPosition;
     v2Pivot = mainCam.WorldToScreenPoint(transform.position);
     v2ScreenStartPos = Input.mousePosition - v2Pivot;
 }
     
 function OnMouseDrag(){
     var v2T : Vector2 = Input.mousePosition - v2Pivot;
     var v3T : Vector3 = v3StartPos;    
     v3T.z = v3T.z - (v2T.y - v2ScreenStartPos.y);
     transform.localPosition = v3T; 
 }

I have two main problems with this code:

  1. I expected v2Pivot to be in [0, screen.dimension] but the values are negativ(!) something like [-90,-100] even so I can see the object. It is completely visible in my screen.

  2. Dragging the Object in the editor leads to z-axis changes between [0,1]. The mousePosition reveals values in [0, screen.dimension]. Hence I need to get the mousePosition relative to the transform.Position. Unfortunately I have no idea who to achieve this.

    Edit

    think I was not clear enough. If I move the mouse 1 pixel upwards, the object will be moved 1 unit in z direction. This is way to "fast". The object should always stay beneath the mouse cursor. So you will not lose the object because it is shooting out of your screen.

Thank you for your help!

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 FL · Jan 27, 2013 at 12:25 AM 0
Share

Try using TransformDirection() with z distance as paramether.

avatar image Paul Graystone · Jan 27, 2013 at 11:11 AM 0
Share

Thank you for the hint. I am currently trying to figure out, where to use TransformDirection(). Can you shed some light onto this?

avatar image FL · Jan 27, 2013 at 12:03 PM 0
Share

A shoot: Change

 v3T.z = v3T.z - (v2T.y - v2ScreenStartPos.y);
 transform.localPosition = v3T; 


to

 transform.localPosition = v3T;
 transform.Translate(0,0,-(v2T.y - v2ScreenStartPos.y));


transform.Translate(0,0,2) make the transform to move 2 z-position along its local z-axis

avatar image Paul Graystone · Jan 27, 2013 at 03:46 PM 0
Share

Thank you for the code. Unfortunately the Object still moves way to fast. If I move the mouse 1 pixel upwards the object moves 1 Unity in z direction. How can I get the object to move with the same "speed" as the mouse. So the object will always stay beneath the mouse cursor?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 27, 2013 at 04:31 PM

Getting the object to move in the Z direction as the same speed as Y movement of the mouse I believe to be a difficult problem. Think about the "base" case of a camera facing down the Z axis with an object at 0.0. There would be no way to keep the mouse on the object since the Y value of the object never moves as it travels along the Z axis. Bringing the camera up, changes things, but still the Z movement is heavily dependent on both the angle of view and the perspective. As an object drifts down the Z axis, it size decreases, so the map between Y movement and Z movement is not linear. Over limited distances, it can be fudged by just adding a factor. The code below has a linear factor. You might be able to improve the result by varying the factor by the distance the object is from the camera. Someone who knows a bit more about the math of perspective might give you an idea of what curve to use, or you could just experiment.

 private var factor : float = 20.0;
 
 private var v3StartPos : Vector3;  // Initial position of object
 private var v3DownPos : Vector3;   // Initial position of the mouse in world coordinates
 private var fZMap : float;         // Base Z distance to use in conversion
  
 function OnMouseDown(){
     v3StartPos = transform.localPosition;
     var v3T : Vector3 = Camera.main.WorldToScreenPoint(v3StartPos);
     fZMap = v3T.z;
     v3DownPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, fZMap);
     v3DownPos = Camera.main.ScreenToWorldPoint(v3DownPos);
 }
  
 function OnMouseDrag(){
     v3T = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, fZMap);
     v3T = Camera.main.ScreenToWorldPoint(v3T);
   
     var v3T2 : Vector3 = v3StartPos;    
     v3T2 = v3StartPos;
     v3T2.x = v3T2.x + (v3T.x - v3DownPos.x);
     v3T2.z = v3T2.z + (v3T.y - v3DownPos.y) * factor; 
     
     transform.localPosition = v3T2; 
 }
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 Paul Graystone · Jan 27, 2013 at 09:14 PM 0
Share

Thank you again for your script. The object doesn't shoot out of the window anymore. In fact it works like a charm.

Facing the problems that come along when using ScreenToWorldPoint would you suggest to use a ray ins$$anonymous$$d to do the job?

avatar image robertbu · Jan 28, 2013 at 01:26 AM 0
Share

I'm not which problem you are referring to with ScreenToWorldPoint. Any problem with ScreenToWorldPoint that I can think of would also occur with Raycasting. Using On$$anonymous$$ouseDown/Drag/Up is object-centric. That is an object knows how to move itself. Note these methods are not available on touch devices. So which to use mostly depends on your game logic and your target device.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

help with mouse scrolling 1 Answer

Orbital cameras and dragging objects with mouse. 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