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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Question regarding the new(ish?) unity input system.

Discussion in 'Scripting' started by Shadowfire638, Jan 31, 2023.

  1. Shadowfire638

    Shadowfire638

    Joined:
    Aug 16, 2019
    Posts:
    10
    I have been working on making a game utilizing the new input system of unity and am getting the hang of it for the most part. However, when it comes to aiming my character's arm I have had no luck, despite searching numerous videos and guides it seems like no matter what I try it never quite works properly with the way my game is laid out. I have the mouse working just fine, but with trying to add in the controller and having them on a control scheme together, I just can't find a way to make it work properly to have the arm rotate because the values that the gamepad and mouse return are just so varied. I've spent hours trying to look through different solutions and guides and haven't found anything that makes sense to me. Any help or advice would be greatly appreciated! And if any additional information is needed as well let me know and I'll get it for you. I included a screenshot of how my action map has the aim assigned as well in case that's part of the problem.



    This is the code:
    Code (CSharp):
    1.  Vector3 screenPoint = CameraController.instance.mainCamera.WorldToScreenPoint(transform.localPosition);
    2.             Debug.Log(screenPoint + "ScreenPoint");
    3.             Vector3 mousePos = Mouse.current.position.ReadValue();
    4.             Vector3 playerPos = transform.position;
    5.             Vector3 aimPos = playerInputActions.Player.Aim.ReadValue<Vector2>();
    6.             Debug.Log(aimPos + "Aim Pos");
    7.  
    8.  
    9.  
    10.             if (mousePos.x < screenPoint.x)
    11.             {
    12.                 transform.localScale = new Vector3(-1f, 1f, 1f);
    13.                 gunArm.localScale = new Vector3(-1f, -1f, 1f);
    14.             }
    15.             else
    16.             {
    17.                 transform.localScale = Vector3.one;
    18.                 gunArm.localScale = Vector3.one;
    19.             }
    20.  
    21.             //rotate gun arm
    22.             Vector2 offset = new Vector2(mousePos.x - screenPoint.x, mousePos.y - screenPoint.y);
    23.             float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
    24.             gunArm.rotation = Quaternion.Euler(0, 0, angle);
    25.  
    26.            // Vector3 aimPoint = CameraController.instance.mainCamera.ScreenToWorldPoint(playerInputActions.Player.Aim.ReadValue<Vector2>());
    27.             //Debug.Log(aimPoint + "AimPoint");
     

    Attached Files:

  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,007
    FYI there's a subforum for the input: https://forum.unity.com/forums/input-system.103/

    By why are you reading mouse position and
    Player.Aim
    separately and trying to useboth? You should be getting input from a single source and work on that alone.
     
  3. Shadowfire638

    Shadowfire638

    Joined:
    Aug 16, 2019
    Posts:
    10
    Oh, my bad didn't know that, I'll try to move it to the appropriate forum.

    As far as the aiming goes I meant to comment out the mouse position, I leave the code in because I know at the least it works, and that way if I get too frustrated trying to make using
    Code (CSharp):
    1. Player.Aim
    work I can turn it back on and have a functional control for aiming while I'm working on other things if that makes sense?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,007
    Well what the current mouse position and the Player.Aim represent are two completely different things. The former is a positional vector, the latter is a directional vector. You can't use them for the same logic.
     
  5. Shadowfire638

    Shadowfire638

    Joined:
    Aug 16, 2019
    Posts:
    10
    That part I understand, the real problem point is more coming from trying to figure out how to use Player.Aim for controller and mouse aiming, but I think what I'm running into is that controller is always going to be a value between -1 and 1, whereas the mouse's value is usually dependent on its position which is a lot harder to control? Maybe I'm just barking up the wrong tree and need to have mouse and controller aiming handled separately...
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,007
    Like I said, take your input from the same source. If you take it from Player.Aim, you get a directional vector, and you can use that to translate a positional vector to determine where to aim your character's arm at or whatever.

    Or, if you want to take it from separate sources, break up the logic. Determine the position from the current mouse position, OR mutate said position with input from Player.Aim, and then aim the arm at said position.
     
  7. Shadowfire638

    Shadowfire638

    Joined:
    Aug 16, 2019
    Posts:
    10
    So coming back a few days later after trying a few more things and I just can' seem to get it to work. I'm using Player.Aim for handling everything now, but the problem I've now run into is that the controller will aim smoothly using that, but once I try to use the mouse it will have an issue based on the type of input used. If reading as mouse(delta) it will violently freak out and shake and be completely unusable. If reading as mouse(position) it will work almost properly, but only from the ends of the screen in comparison to the player. My new functioning code is as such:

    Code (CSharp):
    1.  Vector3 playerPos = transform.position;
    2.             Vector3 lastAim = Vector3.forward;
    3.  
    4.  
    5.             Vector3 aimPos = playerInputActions.Player.Aim.ReadValue<Vector2>();
    6.             Debug.Log(aimPos);
    7.             Vector3 aimPoint = CameraController.instance.mainCamera.ScreenToWorldPoint(aimPos);
    8.          
    9.  
    10.             if (aimPos != Vector3.zero)
    11.             {
    12.                 float aimAngle = Mathf.Atan2(aimPos.y, aimPos.x) * Mathf.Rad2Deg;
    13.                 gunArm.transform.rotation = Quaternion.Euler(0, 0, aimAngle);
    14.  
    15.  
    16.  
    17.                 if (aimPos.x < 0)
    18.                 {
    19.                     transform.localScale = new Vector3(-1f, 1f, 1f);
    20.                     gunArm.localScale = new Vector3(-1f, -1f, 1f);
    21.                 }
    22.                 else
    23.                 {
    24.                     transform.localScale = Vector3.one;
    25.                     gunArm.localScale = Vector3.one;
    26.                 }
    27.             }
    Like I said this mostly works, and I know how to make the mouse work in theory, it's just figuring out what it is I need to do for it to properly work. Using this little bit of script:

    Code (CSharp):
    1.             aimPos.x -= Screen.height / 2;
    2.             aimPos.y -= Screen.height / 2;
    Will properly make the mouse position work. However, due to the way the analog's input works, this will break the aim functionality on controller. I'm sure there is a very simple solution to this that I'm just not understanding due to how new I am so any suggestions or advice would be amazingly helpful!
     
  8. Shadowfire638

    Shadowfire638

    Joined:
    Aug 16, 2019
    Posts:
    10
    So upon further experimentation I finally got it! I ended up basically treating the mouse position as a separate input than the controller after all but honestly since I got both working I can't really complain. Thanks for all the advice it is greatly appreciated!!!