Search Unity

How to do mouse aim for a 3D shooter game?

Discussion in 'Scripting' started by From-Soy-Sauce, Mar 24, 2016.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hello, I'm making a shooter game, that can be seen here:


    Currently I have aiming handled by the 4 arrow keys.

    I was looking up how to do mouse aiming for a third or first person shooter, but the only results I ever got were for things like top-down games.

    So my question is how would I go about getting the mouse input to be used for the reticles movement as well as have the mouse reset to the middle of the screen after every frame?
     
  2. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    would something like this help you? It uses the Mouse X/Y input axis.

    Code (CSharp):
    1.     moveX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
    2.     moveY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
     
  3. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Nope, that's what people use for their top down shooter games, but it doesn't take into account that in an FPS game the character can turn indefinitely.
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Value wise, using arrow keys is no different from get axis mouse x or y, so grimofdoom's solution would have been viable, it would simply be replacing whatever you are doing using arrow keys, with the GetAxis(*InputName*) stuff.

    What's confusing to me is your 2nd part:
    I'm not sure what you are trying to achieve. Is it a third person shooter like this?


    What you have currently looks more like a mobile Shooting gallery which personally, from what's seen in your video looks hella unique. Of course play-testing for how good people feel about it is another issue, but fact remains it differentiates itself from other shooters.
     
  5. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Mobile shooting gallery huh? Oh I understand where you would get that impression.

    Here is the thing: the aiming system is set up to where the picture does not turn until the ret is pushing the edge of the frame, this is a trait that existed in games like Metroid Prime and Kid Icarus Uprising.

    Right now it will move by an amount dependent on the input of the arrow keys.
    Code (CSharp):
    1. AimAdd.x = horizontal_aim;
    2. AimAdd.y = vertical_aim;
    where horizontal aim is -1 when left is held and 1 when right, and respectively similar for vertical.

    But yes, it is a third person shooter like the one you posed, except that the difference in aiming is that in my game that the reticle is not in the center of the game. The reticle is also not at the position of the mouse though, it slides around at an adding to it's angle at an amount dependent on AimAdd.
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Alright from what I've read, the game works the way you want, you just want the controls from arrow keys brought over the the mouse.

    Nothing in the script you provided indicates you are using arrow keys. I'm assuming horizontal_aim and vertical_aim are changed elsewhere. You could easily replace those two values with what grimofdoom wrote, adapted to your needs of course.
     
  7. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Oh.... You are correct.

    Sorry, I did some more digging up, what I was missing was:
    Code (CSharp):
    1. Cursor.lockState = CursorLockMode.Locked;
    My mistake.

    Thanks for the help~
     
    Last edited: Mar 25, 2016