Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

[SOLVED] Custom mouse position origin

Discussion in 'Input System' started by Nylash, Feb 19, 2020.

  1. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    Hi,
    I'm currently making a twin-stick shooter and I got some troubles with mouse position input.
    I need to get the direction between my player, at the center of the screen, and my cursor. The problem is mouse position's origin (0,0) is not the center of the screen but the bottom left corner.
    There is a way to set a custom origin ? Ideally on player's position.
     
    ElnuDev likes this.
  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    Couldn't you simply offset the value of mousePosition ?
     
  3. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    It's a good idea, i tried something and it almost works as intended, here a gif of the result.

    As you can see the little ball, which is my aim object, has a strange behavior.
    There must be an error in my code :
    Code (CSharp):
    1.  
    2. mousePosition -= (Vector2)Camera.main.WorldToScreenPoint(new Vector2(transform.position.x,transform.position.z));
    3. mousePosition.Normalize();
    4. aimGuide.position = transform.position + new Vector3(mousePosition.x, -.5f, mousePosition.y);
    If you have an idea how to do it I take it ^^
     
    Last edited: Feb 20, 2020
  4. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
  5. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    Okay, I found someone with an answser : https://answers.unity.com/questions/932115/top-down-aim-at-mouse.html
    I personally don't use the rotation thing since I only want a direction, so I simply do this :
    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(mousePosition);
    2. Plane plane = new Plane(Vector3.up, Vector3.zero);
    3. float distance;
    4. if (plane.Raycast(ray, out distance))
    5.                 {
    6.                     Vector3 target = ray.GetPoint(distance);
    7.                     aimDirectionMouse = target - transform.position;
    8.                     aimDirectionMouse.Normalize();
    9.                     aimGuide.position = transform.position + new Vector3(aimDirectionMouse.x, -.5f, aimDirectionMouse.z);
    10.                 }
     
    Last edited: Feb 25, 2020
  6. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Yes, but the issue with that is that you're not going to have it nicely bound into one combined action. Ideally, I one would have a single action that is hooked up to both the mouse and joystick if you are going for gamepad support, which unfortunately I don't think is supported by the new Input System yet.
     
  7. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    17
    I think you can't have only one piece of code that handle joystick input and mouse position, but I may be wrong ^^
     
    ElnuDev likes this.