Forum rules - please read before posting.

First Person Camera Input Axis

Hey Chris,

I've tried to sort this myself however I can't seem to achieve the desired outcome. To quickly summarise, I allow the player to use either Mouse/Keyboard or Controller to play the game and in the game there are 2 different control states that the player can find themselves in. These are:
(i) normal First Person movement where moving the mouse/right stick will move the camera around whilst WASD/Left Stick will move the player.
(ii) cursor movement whereby the player can't move at all, the cursor is unlocked and the player can move the cursor around the screen using the mouse/Left Stick.

The problem I'm coming across is one that I've caused myself by wanting to move the cursor (whilst in game state ii) using the left stick instead of the right stick. I realise that it is expected that I should use the right stick on the controller to move the cursor (as it is the right stick that moves the FPS camera during normal movement in state i) however I have scenarios whereby I need the player to click and drag items in state ii and to do so they need to hold down the interaction button whilst also moving the cursor which, due to the interaction button being the 'A' button on the Xbox 360 controller, you cannot hold 'A' down whilst also moving the right stick so I really need to move the cursor with the left stick.

So you've likely guessed where the issue is, cursor movement in state ii is all fine however in state i because I have the Left Stick mapped to CursorVertical (for cursor movement in state ii) when I move the player in state i with the controller when they move forward they look up and when they move back they look down since I have inadvertently mapped movement and cam movement all to the left stick.

I was hoping to simply change the FirstPerson Camera script to use a different InputAxis for camera movement however despite looking through the FPSCam script as well as the _Camera script it inherits from I just can't seem to find any reference to any input axis for camera movement. Your help with this would be greatly appreciated, thank you.

Comments

  • I'm still a little cloudy on the specifics, but essentially you're looking to have CursorHorizontal / CursorVertical be mapped to the left stick in state 2, and to the right stick in state 1?

    You can do this by providing a delegate override to PlayerInput's InputGetAxis function. See the Manual's "Remapping inputs" chapter, but essentially you'd want something like this:

    using UnityEngine;
    using AC;
    
    public class OverrideInputExample : MonoBehaviour
    {
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetAxisDelegate = MyGetAxis;
        }
    
    
        private float MyGetAxis (string axisName)
        {
            if (axisName == "CursorHorizontal")
            {
                if (!KickStarter.playerInput.IsCursorLocked ())
                {
                    // State 2, so read different input
                    return Input.GetAxis ("Horizontal");
                }
            }
            else if (axisName == "CursorVertical")
            {
                if (!KickStarter.playerInput.IsCursorLocked ())
                {
                    // State 2, so read different input
                    return Input.GetAxis ("Vertical");
                }
            }
    
            // return default values
            try
            {
                return Input.GetAxis (axisName);
            }
            catch {}
    
            return 0f;
        }
    
    }
    

    You may need to tweak this, but essentially this will replace all input axis detection. If the axis is CursorHorizontal or CursorVertical, and if the cursor is unlocked, it will instead read Horizontal and Vertical.

  • Hi Chris, thank you for the quick response and help with this. I couldn't seem to get this to work though, I tried adding Debug messages to narrow down why it wasn't working however no luck.

    I have however managed to find what I was looking for after diving down the rabbit hole of AC scripts. Can you advise if what I've done below is an acceptable solution in your expert opinion.

    In the PlayerInput script on line 1653 I have changed the InputGetAxis strings from CursorHoritzontal and CursorVertical to CursorHorizontalCam and CursorVerticalCam. Theses 'Cam' axes are mapped to the mouse as well as the right stick and control the free aim of the camera movement on line 1653 in PlyerInput script only, the movement of the cursor is then controlled by CursorVertical and CursorHorizontal as usual which is mapped to the Mouse and Left Stick and then movement of the player is controlled as usual by the Horizontal and Vertical axes which are mapped to the Left Stick and WASD keys.

    I've literally done this after work so not much time to thoroughly test it however it does work exactly as intended in my limited testing but is there anything you can see being a potential issue with the way I've done this? The only thing I can think of is that I'll obviously need to ensure that I change this line in the script each time I update AC however apart from that is there something obvious that I've missed?

    A huge thanks again for your help.

  • I'm not clear on why my suggested change didn't work, though perhaps I was confused on exactly what it is you're after.

    The function you're modifying can be overridden itself in the same way, though - no need to modify it directly.

    Try this script instead:

    using UnityEngine;
    using AC;
    
    public class OverrideInputExample : MonoBehaviour
    {
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetFreeAimDelegate = FreeAimOverride;
        }
    
        private Vector2 FreeAimOverride (bool cursorIsLocked)
        {
            if (cursorIsLocked)
            {
                return new Vector2 (Input.GetAxis ("CursorHorizontalCam"), Input.GetAxis ("CursorVerticalCam"));
            }
            return Vector2.zero;
        }
    
    }
    
  • Cheers Chris, this seems better than me modifying an AC script so I will look to implement this. Thanks again for your help mate.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.