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
1
Question by THEpancakes · Oct 13, 2012 at 09:46 PM · transformupdatescreentoworldpointinput.mousepositionlateupdate

Calculating change in mouse position

Hi,

I want to be able to move an object based on the movement of my cursor. I don't want it to be placed at the mouse position, or be directly dependent on it; instead I want it to move depending on the difference between the current mousePosition and the last mousePosition.

I am using this code to try to achieve this:

 var cam : Camera;
 var worldPos : Vector3;
 
 function Update() {
 var mousePos = Input.mousePosition;
 mousePos.z = 125;
 worldPos = cam.ScreenToWorldPoint(mousePos);
 }
 
 function LateUpdate() {
 var mousePos2 = Input.mousePosition;
 mousePos2.z = 125;
 var worldPos2 = cam.ScreenToWorldPoint(mousePos2);
 transform.position.x += worldPos2.x - worldPos.x;
 }

But it doesn't seem to work! :( Any ideas what I'm doing wrong?

Thanks in advance. :)

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

Answer by byerdelen · Oct 13, 2012 at 10:05 PM

Update and Late update will give you the same positions. Try getting the mouse value one frame later so make your calculation, place the object and the store the mouse position and use it next time such as :

 function Update() {
 
 worldPos = cam.ScreenToWorldPoint(mousePos);
 mousePos = Vector3(Input.mousePosition.x,Input.mousePosition.y,125);
 
 }

Hope that is the answer you seek

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
3

Answer by AlucardJay · Oct 14, 2012 at 01:27 AM

This script will Debug the changes in the mouse position between frames when you click or hold the mouse button. These values are stored in delta.x and delta.y :

 #pragma strict
 
 public var delta : Vector3 = Vector3.zero;
 private var lastPos : Vector3 = Vector3.zero;
 
 function Update()
 {
     if ( Input.GetMouseButtonDown(0) )
     {
         lastPos = Input.mousePosition;
     }
     else if ( Input.GetMouseButton(0) )
     {
         delta = Input.mousePosition - lastPos;
 
         // Do Stuff here
         
         Debug.Log( "delta X : " + delta.x );
         Debug.Log( "delta Y : " + delta.y );
 
         // End do stuff
 
         lastPos = Input.mousePosition;
     }
 }
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

11 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

Related Questions

Controlling object movement with mouse 0 Answers

What's the execution order of Updates of different objects? 1 Answer

LateUpdate not working properly for UI Text rotation 0 Answers

Trying To Find Mouse Position On Tap 1 Answer

how to achieve a LateFixedUpdate effect 5 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