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 /
avatar image
0
Question by unity_VtempPkrXheVBw · Jan 30, 2019 at 07:32 AM · transform.position

How do I move my object smoothly using mouse,How do I make the object movement smooth ?

I want my object to move only when it is clicked on by the mouse and the mouse remains clicked. Once clicked, the object should move with the mouse wherever it goes.

I use the following code but the movement is not smooth and sometimes the object moves away from the mouse's location, usually lagging behind. Ive put this code in the FixedUpdate() function.

     if (Input.GetMouseButton(0))
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

         if (Physics.Raycast(ray, out hit, Mathf.Infinity))
         {
             if (hit.transform.name == "pocketofcoin")
             {
                 float moveLR = Input.GetAxis("Mouse X") * mouseSensitivityX;
                 float moveUD = Input.GetAxis("Mouse Y") * mouseSensitivityY;

                 float _x = transform.position.x + moveLR;
                 float _y = transform.position.y + moveUD;

                 transform.position = new Vector2(_x, _y);
             }
         }
     }
Comment
Add comment · Show 3
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 bpaynom · Jan 30, 2019 at 01:38 PM 0
Share

Don't forget to accept the answer, not just vote it. Thanks ;)

avatar image Captain_Pineapple · Jan 30, 2019 at 03:11 PM 0
Share

I'm not sure if either of the answers is really the thing to fix your problem. You should probably rather look into getting the absolute mouse position on screen and transfor$$anonymous$$g that to a position that you want your object to be. Also don't forget to take your camera perspective into account and how that affects a object position in relation to the mouse if its not exactly in screenspace.

avatar image xxmariofer Captain_Pineapple · Jan 30, 2019 at 03:15 PM 0
Share

he said that his code was working but wasnt moving smoothly, and the easiest solution is rather than teleporting to the position the user clicked (like he is doing and and you suggested) is to movetowards the position, using the vector3.movetowards, in a linear velocity way.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bpaynom · Jan 30, 2019 at 08:09 AM

It's a bad idea to put Input in FixedUpdate() because it's not called every frame, so you lose some input.


Second, there are these functions that may help you OnMouseDown , OnMouseDrag , OnMouseUp.

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
1

Answer by xxmariofer · Jan 30, 2019 at 08:18 AM

Like @ConcanoPayno pointed, dont catch inputs in the fixedupdate. but what y9u are looking for is something like https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html rather thans translating the oobject to one specific point.

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

100 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find a transform with name in prefab 2 Answers

Calculating where to position instantiated clone 1 Answer

How to make the camera acquire the position of a gameobject and start following it? 1 Answer

collider position wrong object position in collider 0 Answers

Change position of an object on a keystroke within a trigger only? 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