Making an Object Follow the Mouse in Visual Studio C#


Making an Object Follow the Mouse in Visual Studio C#

Hello friends, today I will tell you how you can make any object follow the mouse using C# in Visual Studio.

There are 2 methods for this;
1st Method: Instantly matching the position of the object to the mouse position.
2nd Method: Making the object proceed towards the mouse by incrementally adjusting its position.

The main difference between these two methods is that in one the object teleports, while in the other the object moves gradually.


1st Method: Teleport the Object to the Mouse Position

To make the object follow the mouse by constantly teleporting to the mouse's position, first add a Timer to your project and then add the object you want to control to your form.


Now all you have to do is click on the Timer and write the necessary commands.

 private void timer1_Tick(object sender, EventArgs e)
        {
            // Get the position of the mouse
            int x = Cursor.Position.X;
            int y = Cursor.Position.Y;

            /* To make the object go directly */

            // Directly set the object's position to the mouse's position.
            pictureBox1.Location = new Point(x, y);

            /* To make the object go directly */
        }


Actually, there are enough comment lines in the code, but let's explain it a bit more.
With int x, we get the position of the mouse on the x-axis using Cursor.position.x and did the same for the y function.
Then we said we want to change the object's position with location and set the object's position to the mouse's x and y position using new point(x,y).

Make sure the Timer's enabled property is true for the commands to work.


2nd Method: Make the Object Progress Towards the Mouse Position

In this method, instead of making the object go directly to the mouse, you make it move gradually.
We add the timer and our object to our project as we did in the 1st method.


Then we enter the necessary commands.

  private void timer1_Tick(object sender, EventArgs e)
        {
            // Get the position of the mouse
            int x = Cursor.Position.X;
            int y = Cursor.Position.Y;

            /* To make the object go directly */

            // Directly set the object's position to the mouse's position.
            pictureBox1.Location = new Point(x, y);

            /* To make the object go directly */

            /* 2nd Method: To make the object move gradually */

            // Get the object's current position
            int px = pictureBox1.Location.X;
            int py = pictureBox1.Location.Y;

            // Make the object move gradually towards the mouse with incremental changes

            if(x > px)
            {
                
                pictureBox1.Left += 1;
            }
            else if(x < px)
            {
              
                pictureBox1.Left -= 1;
            }
            else
            {

            }

             else if(y < py)
            {
               
                pictureBox1.Top -= 1;
            }
            else
            {

            }

            /* To make the object move gradually */
        }

First, we got the mouse's x and y coordinates and assigned them to the x and y variables. Then we took the object's current position and assigned them to px and py variables.
Finally, with the if - else system we created, we made the object move towards the mouse.
.left += 1 and .left -= 1 specify how much the object should move to the right and left.
.top += 1 and .top -= 1 determine how much the object moves up and down.

Make sure your Timer's enabled property is true and its interval is set to a very low value for best results so that your code works.


If you want to see the version of the project that I made, you can download it from GitHub.
GitHub - Make the Object Follow the Mouse

Video While Working