Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Custom mouse position origin

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

  1. Nylash

    Nylash

    Joined:
    Dec 4, 2017
    Posts:
    16
    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:
    16
    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:
    16
    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:
    16
    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.