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. Dismiss Notice

FPSController does not work in Universal 10

Discussion in 'Windows' started by Siliwinter, Aug 31, 2016.

  1. Siliwinter

    Siliwinter

    Joined:
    Dec 6, 2015
    Posts:
    15
    So with my existing project i wanted to try it out in UWP format so i switched platforms from PC MAC AND LINUX to WINDOWS STORE UNIVERSAL 10.

    After trying to play my game i can't move my fpscontroller, The movement and mouse are not working but other keyboard functions like the Menu and Inventory are working as-well as the mouse input for the Menu.

    IS there any code i have to change or does the Unity Standard FPSControler not work with Universal 10?

    Thanks!

    (if this sound rushed sorry i was on the bus.)
     
  2. Siliwinter

    Siliwinter

    Joined:
    Dec 6, 2015
    Posts:
    15
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,478
    Check the defines/code. It could be defaulting to touch input.
     
  4. Siliwinter

    Siliwinter

    Joined:
    Dec 6, 2015
    Posts:
    15
    Could you be more specific? this is the code

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4. using UnityStandardAssets.Utility;
    5. using Random = UnityEngine.Random;
    6.  
    7. namespace UnityStandardAssets.Characters.FirstPerson
    8. {
    9.     [RequireComponent(typeof (CharacterController))]
    10.     [RequireComponent(typeof (AudioSource))]
    11.     public class FirstPersonController : MonoBehaviour
    12.     {
    13.         [SerializeField] private bool m_IsWalking;
    14.         [SerializeField] private float m_WalkSpeed;
    15.         [SerializeField] private float m_RunSpeed;
    16.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    17.         [SerializeField] private float m_JumpSpeed;
    18.         [SerializeField] private float m_StickToGroundForce;
    19.         [SerializeField] private float m_GravityMultiplier;
    20.         [SerializeField] private MouseLook m_MouseLook;
    21.         [SerializeField] private bool m_UseFovKick;
    22.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
    23.         [SerializeField] private bool m_UseHeadBob;
    24.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    25.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    26.         [SerializeField] private float m_StepInterval;
    27.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    28.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    29.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    30.  
    31.         private Camera m_Camera;
    32.         private bool m_Jump;
    33.         private float m_YRotation;
    34.         private Vector2 m_Input;
    35.         private Vector3 m_MoveDir = Vector3.zero;
    36.         private CharacterController m_CharacterController;
    37.         private CollisionFlags m_CollisionFlags;
    38.         private bool m_PreviouslyGrounded;
    39.         private Vector3 m_OriginalCameraPosition;
    40.         private float m_StepCycle;
    41.         private float m_NextStep;
    42.         private bool m_Jumping;
    43.         private AudioSource m_AudioSource;
    44.  
    45.         // Use this for initialization
    46.         private void Start()
    47.         {
    48.             m_CharacterController = GetComponent<CharacterController>();
    49.             m_Camera = Camera.main;
    50.             m_OriginalCameraPosition = m_Camera.transform.localPosition;
    51.             m_FovKick.Setup(m_Camera);
    52.             m_HeadBob.Setup(m_Camera, m_StepInterval);
    53.             m_StepCycle = 0f;
    54.             m_NextStep = m_StepCycle/2f;
    55.             m_Jumping = false;
    56.             m_AudioSource = GetComponent<AudioSource>();
    57.             m_MouseLook.Init(transform , m_Camera.transform);
    58.         }
    59.  
    60.  
    61.         // Update is called once per frame
    62.         private void Update()
    63.         {
    64.             RotateView();
    65.             // the jump state needs to read here to make sure it is not missed
    66.             if (!m_Jump)
    67.             {
    68.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    69.             }
    70.  
    71.             if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    72.             {
    73.                 StartCoroutine(m_JumpBob.DoBobCycle());
    74.                 PlayLandingSound();
    75.                 m_MoveDir.y = 0f;
    76.                 m_Jumping = false;
    77.             }
    78.             if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    79.             {
    80.                 m_MoveDir.y = 0f;
    81.             }
    82.  
    83.             m_PreviouslyGrounded = m_CharacterController.isGrounded;
    84.         }
    85.  
    86.  
    87.         private void PlayLandingSound()
    88.         {
    89.             m_AudioSource.clip = m_LandSound;
    90.             m_AudioSource.Play();
    91.             m_NextStep = m_StepCycle + .5f;
    92.         }
    93.  
    94.  
    95.         private void FixedUpdate()
    96.         {
    97.             float speed;
    98.             GetInput(out speed);
    99.             // always move along the camera forward as it is the direction that it being aimed at
    100.             Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
    101.  
    102.             // get a normal for the surface that is being touched to move along it
    103.             RaycastHit hitInfo;
    104.             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    105.                                m_CharacterController.height/2f, ~0, QueryTriggerInteraction.Ignore);
    106.             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
    107.  
    108.             m_MoveDir.x = desiredMove.x*speed;
    109.             m_MoveDir.z = desiredMove.z*speed;
    110.  
    111.  
    112.             if (m_CharacterController.isGrounded)
    113.             {
    114.                 m_MoveDir.y = -m_StickToGroundForce;
    115.  
    116.                 if (m_Jump)
    117.                 {
    118.                     m_MoveDir.y = m_JumpSpeed;
    119.                     PlayJumpSound();
    120.                     m_Jump = false;
    121.                     m_Jumping = true;
    122.                 }
    123.             }
    124.             else
    125.             {
    126.                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    127.             }
    128.             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
    129.  
    130.             ProgressStepCycle(speed);
    131.             UpdateCameraPosition(speed);
    132.  
    133.             m_MouseLook.UpdateCursorLock();
    134.         }
    135.  
    136.  
    137.         private void PlayJumpSound()
    138.         {
    139.             m_AudioSource.clip = m_JumpSound;
    140.             m_AudioSource.Play();
    141.         }
    142.  
    143.  
    144.         private void ProgressStepCycle(float speed)
    145.         {
    146.             if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    147.             {
    148.                 m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    149.                              Time.fixedDeltaTime;
    150.             }
    151.  
    152.             if (!(m_StepCycle > m_NextStep))
    153.             {
    154.                 return;
    155.             }
    156.  
    157.             m_NextStep = m_StepCycle + m_StepInterval;
    158.  
    159.             PlayFootStepAudio();
    160.         }
    161.  
    162.  
    163.         private void PlayFootStepAudio()
    164.         {
    165.             if (!m_CharacterController.isGrounded)
    166.             {
    167.                 return;
    168.             }
    169.             // pick & play a random footstep sound from the array,
    170.             // excluding sound at index 0
    171.             int n = Random.Range(1, m_FootstepSounds.Length);
    172.             m_AudioSource.clip = m_FootstepSounds[n];
    173.             m_AudioSource.PlayOneShot(m_AudioSource.clip);
    174.             // move picked sound to index 0 so it's not picked next time
    175.             m_FootstepSounds[n] = m_FootstepSounds[0];
    176.             m_FootstepSounds[0] = m_AudioSource.clip;
    177.         }
    178.  
    179.  
    180.         private void UpdateCameraPosition(float speed)
    181.         {
    182.             Vector3 newCameraPosition;
    183.             if (!m_UseHeadBob)
    184.             {
    185.                 return;
    186.             }
    187.             if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    188.             {
    189.                 m_Camera.transform.localPosition =
    190.                     m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    191.                                       (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    192.                 newCameraPosition = m_Camera.transform.localPosition;
    193.                 newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    194.             }
    195.             else
    196.             {
    197.                 newCameraPosition = m_Camera.transform.localPosition;
    198.                 newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    199.             }
    200.             m_Camera.transform.localPosition = newCameraPosition;
    201.         }
    202.  
    203.  
    204.         private void GetInput(out float speed)
    205.         {
    206.             // Read input
    207.             float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    208.             float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    209.  
    210.             bool waswalking = m_IsWalking;
    211.  
    212. #if !MOBILE_INPUT
    213.             // On standalone builds, walk/run speed is modified by a key press.
    214.             // keep track of whether or not the character is walking or running
    215.             m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    216. #endif
    217.             // set the desired speed to be walking or running
    218.             speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    219.             m_Input = new Vector2(horizontal, vertical);
    220.  
    221.             // normalize input if it exceeds 1 in combined length:
    222.             if (m_Input.sqrMagnitude > 1)
    223.             {
    224.                 m_Input.Normalize();
    225.             }
    226.  
    227.             // handle speed change to give an fov kick
    228.             // only if the player is going to a run, is running and the fovkick is to be used
    229.             if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    230.             {
    231.                 StopAllCoroutines();
    232.                 StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    233.             }
    234.         }
    235.  
    236.  
    237.         private void RotateView()
    238.         {
    239.             m_MouseLook.LookRotation (transform, m_Camera.transform);
    240.         }
    241.  
    242.  
    243.         private void OnControllerColliderHit(ControllerColliderHit hit)
    244.         {
    245.             Rigidbody body = hit.collider.attachedRigidbody;
    246.             //dont move the rigidbody if the character is on top of it
    247.             if (m_CollisionFlags == CollisionFlags.Below)
    248.             {
    249.                 return;
    250.             }
    251.  
    252.             if (body == null || body.isKinematic)
    253.             {
    254.                 return;
    255.             }
    256.             body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    257.         }
    258.     }
    259. }
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,478
    Are you sure that's the code responsible for not processing input correctly? I don't see any input related code at all in it.
     
  6. Siliwinter

    Siliwinter

    Joined:
    Dec 6, 2015
    Posts:
    15
    What about this?
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput.PlatformSpecific;
    4.  
    5. namespace UnityStandardAssets.CrossPlatformInput
    6. {
    7.     public static class CrossPlatformInputManager
    8.     {
    9.         public enum ActiveInputMethod
    10.         {
    11.             Hardware,
    12.             Touch
    13.         }
    14.  
    15.  
    16.         private static VirtualInput activeInput;
    17.  
    18.         private static VirtualInput s_TouchInput;
    19.         private static VirtualInput s_HardwareInput;
    20.  
    21.  
    22.         static CrossPlatformInputManager()
    23.         {
    24.             s_TouchInput = new MobileInput();
    25.             s_HardwareInput = new StandaloneInput();
    26. #if MOBILE_INPUT
    27.             activeInput = s_TouchInput;
    28. #else
    29.             activeInput = s_HardwareInput;
    30. #endif
    31.         }
    32.  
    33.         public static void SwitchActiveInputMethod(ActiveInputMethod activeInputMethod)
    34.         {
    35.             switch (activeInputMethod)
    36.             {
    37.                 case ActiveInputMethod.Hardware:
    38.                     activeInput = s_HardwareInput;
    39.                     break;
    40.  
    41.                 case ActiveInputMethod.Touch:
    42.                     activeInput = s_TouchInput;
    43.                     break;
    44.             }
    45.         }
    46.  
    47.         public static bool AxisExists(string name)
    48.         {
    49.             return activeInput.AxisExists(name);
    50.         }
    51.  
    52.         public static bool ButtonExists(string name)
    53.         {
    54.             return activeInput.ButtonExists(name);
    55.         }
    56.  
    57.         public static void RegisterVirtualAxis(VirtualAxis axis)
    58.         {
    59.             activeInput.RegisterVirtualAxis(axis);
    60.         }
    61.  
    62.  
    63.         public static void RegisterVirtualButton(VirtualButton button)
    64.         {
    65.             activeInput.RegisterVirtualButton(button);
    66.         }
    67.  
    68.  
    69.         public static void UnRegisterVirtualAxis(string name)
    70.         {
    71.             if (name == null)
    72.             {
    73.                 throw new ArgumentNullException("name");
    74.             }
    75.             activeInput.UnRegisterVirtualAxis(name);
    76.         }
    77.  
    78.  
    79.         public static void UnRegisterVirtualButton(string name)
    80.         {
    81.             activeInput.UnRegisterVirtualButton(name);
    82.         }
    83.  
    84.  
    85.         // returns a reference to a named virtual axis if it exists otherwise null
    86.         public static VirtualAxis VirtualAxisReference(string name)
    87.         {
    88.             return activeInput.VirtualAxisReference(name);
    89.         }
    90.  
    91.  
    92.         // returns the platform appropriate axis for the given name
    93.         public static float GetAxis(string name)
    94.         {
    95.             return GetAxis(name, false);
    96.         }
    97.  
    98.  
    99.         public static float GetAxisRaw(string name)
    100.         {
    101.             return GetAxis(name, true);
    102.         }
    103.  
    104.  
    105.         // private function handles both types of axis (raw and not raw)
    106.         private static float GetAxis(string name, bool raw)
    107.         {
    108.             return activeInput.GetAxis(name, raw);
    109.         }
    110.  
    111.  
    112.         // -- Button handling --
    113.         public static bool GetButton(string name)
    114.         {
    115.             return activeInput.GetButton(name);
    116.         }
    117.  
    118.  
    119.         public static bool GetButtonDown(string name)
    120.         {
    121.             return activeInput.GetButtonDown(name);
    122.         }
    123.  
    124.  
    125.         public static bool GetButtonUp(string name)
    126.         {
    127.             return activeInput.GetButtonUp(name);
    128.         }
    129.  
    130.  
    131.         public static void SetButtonDown(string name)
    132.         {
    133.             activeInput.SetButtonDown(name);
    134.         }
    135.  
    136.  
    137.         public static void SetButtonUp(string name)
    138.         {
    139.             activeInput.SetButtonUp(name);
    140.         }
    141.  
    142.  
    143.         public static void SetAxisPositive(string name)
    144.         {
    145.             activeInput.SetAxisPositive(name);
    146.         }
    147.  
    148.  
    149.         public static void SetAxisNegative(string name)
    150.         {
    151.             activeInput.SetAxisNegative(name);
    152.         }
    153.  
    154.  
    155.         public static void SetAxisZero(string name)
    156.         {
    157.             activeInput.SetAxisZero(name);
    158.         }
    159.  
    160.  
    161.         public static void SetAxis(string name, float value)
    162.         {
    163.             activeInput.SetAxis(name, value);
    164.         }
    165.  
    166.  
    167.         public static Vector3 mousePosition
    168.         {
    169.             get { return activeInput.MousePosition(); }
    170.         }
    171.  
    172.  
    173.         public static void SetVirtualMousePositionX(float f)
    174.         {
    175.             activeInput.SetVirtualMousePositionX(f);
    176.         }
    177.  
    178.  
    179.         public static void SetVirtualMousePositionY(float f)
    180.         {
    181.             activeInput.SetVirtualMousePositionY(f);
    182.         }
    183.  
    184.  
    185.         public static void SetVirtualMousePositionZ(float f)
    186.         {
    187.             activeInput.SetVirtualMousePositionZ(f);
    188.         }
    189.  
    190.  
    191.         // virtual axis and button classes - applies to mobile input
    192.         // Can be mapped to touch joysticks, tilt, gyro, etc, depending on desired implementation.
    193.         // Could also be implemented by other input devices - kinect, electronic sensors, etc
    194.         public class VirtualAxis
    195.         {
    196.             public string name { get; private set; }
    197.             private float m_Value;
    198.             public bool matchWithInputManager { get; private set; }
    199.  
    200.  
    201.             public VirtualAxis(string name)
    202.                 : this(name, true)
    203.             {
    204.             }
    205.  
    206.  
    207.             public VirtualAxis(string name, bool matchToInputSettings)
    208.             {
    209.                 this.name = name;
    210.                 matchWithInputManager = matchToInputSettings;
    211.             }
    212.  
    213.  
    214.             // removes an axes from the cross platform input system
    215.             public void Remove()
    216.             {
    217.                 UnRegisterVirtualAxis(name);
    218.             }
    219.  
    220.  
    221.             // a controller gameobject (eg. a virtual thumbstick) should update this class
    222.             public void Update(float value)
    223.             {
    224.                 m_Value = value;
    225.             }
    226.  
    227.  
    228.             public float GetValue
    229.             {
    230.                 get { return m_Value; }
    231.             }
    232.  
    233.  
    234.             public float GetValueRaw
    235.             {
    236.                 get { return m_Value; }
    237.             }
    238.         }
    239.  
    240.         // a controller gameobject (eg. a virtual GUI button) should call the
    241.         // 'pressed' function of this class. Other objects can then read the
    242.         // Get/Down/Up state of this button.
    243.         public class VirtualButton
    244.         {
    245.             public string name { get; private set; }
    246.             public bool matchWithInputManager { get; private set; }
    247.  
    248.             private int m_LastPressedFrame = -5;
    249.             private int m_ReleasedFrame = -5;
    250.             private bool m_Pressed;
    251.  
    252.  
    253.             public VirtualButton(string name)
    254.                 : this(name, true)
    255.             {
    256.             }
    257.  
    258.  
    259.             public VirtualButton(string name, bool matchToInputSettings)
    260.             {
    261.                 this.name = name;
    262.                 matchWithInputManager = matchToInputSettings;
    263.             }
    264.  
    265.  
    266.             // A controller gameobject should call this function when the button is pressed down
    267.             public void Pressed()
    268.             {
    269.                 if (m_Pressed)
    270.                 {
    271.                     return;
    272.                 }
    273.                 m_Pressed = true;
    274.                 m_LastPressedFrame = Time.frameCount;
    275.             }
    276.  
    277.  
    278.             // A controller gameobject should call this function when the button is released
    279.             public void Released()
    280.             {
    281.                 m_Pressed = false;
    282.                 m_ReleasedFrame = Time.frameCount;
    283.             }
    284.  
    285.  
    286.             // the controller gameobject should call Remove when the button is destroyed or disabled
    287.             public void Remove()
    288.             {
    289.                 UnRegisterVirtualButton(name);
    290.             }
    291.  
    292.  
    293.             // these are the states of the button which can be read via the cross platform input system
    294.             public bool GetButton
    295.             {
    296.                 get { return m_Pressed; }
    297.             }
    298.  
    299.  
    300.             public bool GetButtonDown
    301.             {
    302.                 get
    303.                 {
    304.                     return m_LastPressedFrame - Time.frameCount == -1;
    305.                 }
    306.             }
    307.  
    308.  
    309.             public bool GetButtonUp
    310.             {
    311.                 get
    312.                 {
    313.                     return (m_ReleasedFrame == Time.frameCount - 1);
    314.                 }
    315.             }
    316.         }
    317.     }
    318. }
    319.  
    This is the cross platform input manager code.
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,478
    So... does MOBILE_INPUT end up being defined? Because if it does, it will expect touch input.