- Home /
Problem is not reproducible or outdated
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.
Well anything's possible, but this is a bit vague. Can you draw a picture or elaborate more on what your situation is?
You can take a look at my other comment. I hope that explains it.
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.
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.
So, if I click on an object an move the mouse, the object should stay under the cursor.
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;
}
Thanks for the input. The code does not work. Camera.main.tranform is read only and even if I use Translate, nothing happens.
Change the line that is complaining to: Camera.main.transform.position -= worldDisplacement;
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.
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.
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);
}
Follow this Question
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