N
N
Nikita Podosenov2020-12-02 17:54:21
C++ / C#
Nikita Podosenov, 2020-12-02 17:54:21

How to make the GameObject follow the cursor without offset?

There is a code

void Update()
    {
        //получить текущие координаты мыши на экране из Input
        Vector3 mousePos2D = Input.mousePosition;

        //координата Z камеры определяет, как далеко в трехмерном пространстве находится указатель мыши
        mousePos2D.z = -Camera.main.transform.position.z;

        //преобразовать точку на двухмерной плоскости экрана в трехмерные кординаты игры
        Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);

        //переместить корзину вдоль оси X в координату X указателя мыши
        Vector3 pos = this.transform.position;
        pos.x = mousePos2D.x;
        this.transform.position = pos;
    }

Its job is to move the game object exactly behind the cursor on the x-axis, but instead it shifts a lot. How can I make it strictly follow the cursor?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
parovoz778, 2020-12-04
@parovoz778

Displacement - do I understand jamming? When the mouse moves and the object stays in place?
Then you need more frames per second

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question