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 /
This question was closed Apr 27, 2018 at 09:50 AM by keckluis for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by keckluis · Apr 25, 2018 at 11:33 AM · cameraobjectcamera-movementdragmouseposition

Make object follow mouse, while actually moving camera, instead of object

So, I need to drag objects around, without actually moving the objects. I want to do this by moving the camera in a way that it looks like the object follows the mouse position. Is that possible? I am not very experienced.

Thanks in advance.

Comment
Add comment · Show 17
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 TreyH · Apr 25, 2018 at 12:53 PM 0
Share

Well anything's possible, but this is a bit vague. Can you draw a picture or elaborate more on what your situation is?

avatar image keckluis TreyH · Apr 25, 2018 at 01:36 PM 0
Share

You can take a look at my other comment. I hope that explains it.

avatar image Geejayz · Apr 25, 2018 at 01:20 PM 0
Share

Wondering if you mean that you want to 1. $$anonymous$$ove the object with the mouse pointer around a defined area when selected with the mouse button held down 2. $$anonymous$$aintain the camera centered over the mouse pointer position whilst doing this. 3. On release of the mouse button, the object retains the transform position you moved it to but you can now go back to moving around the defined area with the mouse and the camera follows the mouse pointer whilst you do this.

avatar image keckluis Geejayz · Apr 25, 2018 at 01:35 PM 0
Share

I want the object to keep its position, but it should look like I am dragging the object with my mouse, even though I am actually moving the camera.

avatar image keckluis keckluis · Apr 25, 2018 at 01:38 PM 0
Share

So, if I click on an object an move the mouse, the object should stay under the cursor.

avatar image Harinezumi · Apr 25, 2018 at 02:10 PM 2
Share

The approach for if you were trying to move the object, is to check the displacement of the mouse from one frame to the other, transform that into world space (Camera.ScreenToWorldPoint() can help in that), then move the object that amount. So as an idea, have you tried applying the reverse of that displacement to the camera? I don't know if it works, but here is some code:

 private Vector3 previous$$anonymous$$ousePosition;
 
 private void Update () {
     Vector3 current$$anonymous$$ousePosition = Input.mousePosition;
     if (Input.Get$$anonymous$$ouseButton(0)) { // left mouse button held
         Vector3 previousWorldPosition = Camera.main.ScreenToWorldPoint(previous$$anonymous$$ousePosition);
         Vector3 currentWorldPosition = Camera.main.ScreenToWorldPoint(current$$anonymous$$ousePosition);
         Vector3 worldDisplacement = currentWorldPosition - previousWorldPosition;
         Camera.main.transform.position -= worldDisplacement;
     }
     previous$$anonymous$$ousePosition = current$$anonymous$$ousePosition;
 }

avatar image keckluis Harinezumi · Apr 25, 2018 at 02:42 PM 0
Share

Thanks for the input. The code does not work. Camera.main.tranform is read only and even if I use Translate, nothing happens.

avatar image Scribe keckluis · Apr 25, 2018 at 05:14 PM 1
Share

Change the line that is complaining to: Camera.main.transform.position -= worldDisplacement;

Show more comments
Show more comments

2 Replies

  • Sort: 
avatar image
0

Answer by Kudorado · Apr 25, 2018 at 02:09 PM

Hi, you can add the parent of object as camera, then moving camera and object also moving otherwise the local position of object never change.

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 keckluis · Apr 25, 2018 at 02:45 PM 0
Share

Sorry, but I don't really unserstand what you are trying to say. Do you mean, that I should make the object a child of the camera? If yes, that would mean that the Object always stays on the same screen position. That's not what I am looking for.

avatar image
0

Answer by petarpejovic · Apr 26, 2018 at 08:22 AM

Hi :) This is simple script for making Camera follow Mouse position. You can try with this and also you will probably need to edit it for your purpose.

 public class FollowMouse : MonoBehaviour {
 
     private Vector3 mousePosition;
     public float moveSpeed = 0.1f;
 
     void Update ()
     {
             mousePosition = Input.mousePosition;
             mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
             transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
     }

 
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

Follow this Question

Answers Answers and Comments

142 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 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

Drag Camera Around an Object With Kinect (Official Plug-In) 0 Answers

Why is Input.mousePosition returning wired values using Cinemachine 2D with dynamic following? 0 Answers

Is it good idea to implement camera drag with UI drag event? 2 Answers

How can I set up cameras to track the player from room to room? 1 Answer

Have Arms follow camera partially 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