How to Make Player moving towards mouse in unity 3d via C#

How to Make Player moving towards mouse in unity 3d via C#

Method 1:

Here is one way to make a player move towards the mouse in Unity 3D using C#:

  1. First, create a new C# script and name it "PlayerController".

  2. Add the following code to the "PlayerController" script:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // The speed at which the player moves
    public float moveSpeed = 5f;

    // Update is called once per frame
    void Update()
    {
        // Get the mouse position in screen space
        Vector3 mousePosition = Input.mousePosition;

        // Convert the mouse position to world space
        Vector3 mousePositionInWorld =
Camera.main.ScreenToWorldPoint(mousePosition);

        // Set the player's position to the mouse position
        transform.position = Vector3.Lerp(transform.position,
mousePositionInWorld, moveSpeed * Time.deltaTime);
    }
}

  1. In your Unity scene, attach the "PlayerController" script to the player object.

  2. Adjust the "moveSpeed" variable in the Inspector to control how fast the player moves towards the mouse.

That's it! This script will cause the player to move towards the mouse cursor, with the speed determined by the "moveSpeed" variable.

If you want to make the player move towards the mouse in a 2D game, you can use the same basic approach, but use the Rigidbody2D.MovePosition() method to move the player instead of setting the transform.position directly. This will allow you to use physics forces and collisions in your 2D game.

Method 2:

To make a player character move towards the mouse cursor in Unity, you need to use the Vector3.Lerp method to smoothly move the player towards the cursor. Here's an example of how you might do this:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f; // The speed at which the player moves

    private void Update()
    {
        // Get the current mouse position in screen coordinates
        Vector3 mousePosition = Input.mousePosition;

        // Convert the mouse position to world coordinates
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mousePosition);

        // Set the z coordinate of the mouse world position to the same as the player's z coordinate
        mouseWorldPosition.z = transform.position.z;

        // Use Vector3.Lerp to smoothly move the player towards the mouse position
        transform.position = Vector3.Lerp(transform.position, mouseWorldPosition, Time.deltaTime * speed);
    }
}

This code will move the player character towards the mouse cursor at a speed of 5.0f units per second. You can adjust the speed variable to control the movement speed of the player.

Method 3:

To make a player move towards the mouse in Unity, you can use the Vector3.Lerp method to smoothly move the player towards the position of the mouse. Here is an example of how you could do this in C#:

using UnityEngine;


public class PlayerMovement : MonoBehaviour

{

    public float speed = 5.0f; // The speed at which the player will move


   void Update()


    {

        // Get the mouse position in world space


    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);


       // Calculate the direction from the player to the mouse

        Vector3 direction = (mousePos - transform.position).normalized;


        // Move the player towards the mouse using the calculated direction

        transform.position = Vector3.Lerp(transform.position, mousePos, speed *

Time.deltaTime);

   }

}

In this script, we first get the position of the mouse in world space using the ScreenToWorldPoint method. We then calculate the direction from the player to the mouse by subtracting the player's position from the mouse position and normalizing the result. Finally, we use the Lerp method to smoothly move the player towards the mouse position by the given speed.

You can then attach this script to your player object in Unity, and it should move towards the mouse when you run the game.