Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to make my Rigidbody move towards mouse position on the screen

Discussion in 'Scripting' started by HenrikoMagnifico, Aug 26, 2018.

  1. HenrikoMagnifico

    HenrikoMagnifico

    Joined:
    Nov 25, 2014
    Posts:
    4
    Hey! I'm trying to make my Rigidbody move (using AddForce) towards the mouse cursor position on the screen. Here is the code I've got at the moment;

    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.                 {
    3.                     var worldMousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0f, Input.mousePosition.z));
    4.  
    5.                     var direction = worldMousePosition - transform.position;
    6.  
    7.                     rb.AddForce((direction).normalized * 150 * Time.deltaTime);
    8.                     Debug.Log("Direction: " + direction);
    9.                 }
    In my head, this should work just fine. It sets the mouse position Vector3 to the correct point in the world space relative to where it is on the screen (it is a top down game, therefor the Y-position is set to 0) then subtracts the player's position Vector with the mouse position and then applies that Vector as a force on the player object. At least, that's what it should do. Right now the player won't move at all. What am I doing wrong?

    Thanks in advance!
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Your force is probably not substantial enough. 150 * a normal 60fps deltaTime is ~2.4. Try cranking it up a notch.
     
  3. HenrikoMagnifico

    HenrikoMagnifico

    Joined:
    Nov 25, 2014
    Posts:
    4
    Alright I made some changes and increased the force and now it actually moves. But it can only move up and down and won't move to the mouse position at all. This is the new code I've got;
    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.                 {
    3.                     var mousePos = Input.mousePosition;
    4.                     mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    5.  
    6.                     var mouseDir = mousePos - gameObject.transform.position;
    7.                     mouseDir = new Vector3(mouseDir.x, 0f, mouseDir.z);
    8.                     mouseDir = mouseDir.normalized;
    9.  
    10.                     rb.AddForce((mouseDir) * 1500f * Time.smoothDeltaTime);
    11.  
    12.                     Debug.Log("Direction: " + mousePos);
    13.                 }
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You need to learn to debug through your code. Try using the debugger or add debug logging.

    To start with, are any lines there not working as you are expecting (there is certainly one :))? Once you find something that is not working as you expect you can set about finding out why and thereby fixing it.

    Incidentally- it is recommended to cache the return from
    Camera.main
    rather than keep calling it.