Search Unity

Can I control a camera using the Joystick hat to look around in a starship or aircraft cockpit?

Discussion in 'Scripting' started by Vampyr_Engel, Feb 7, 2017.

  1. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Yes How would I be able to do that using the Joystick commands? I know it might become obsolete because of Virtual Reality Headsets and IR tracking but for those causl gamers how can I set this up in Unity?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,702
    You can set up the joystick hat via Edit -> Project Settings -> Input. Create two new axes (one for vertical and one for horizontal), and then figure out which joy axis (via the dropdown next to "Axis") is the right one. Then you can use Input.GetAxis to get that input in game.

    TBCH this is a terrible solution, as your players will have different joysticks that will map their hat to different axes, in addition to being a major pain to set up in the first place. Unity's current joystick input system is awful, and it's been in the works for a while to overhaul the Input system entirely, and it looks like you can grab an experimental preview for that if you'd rather play with experimental software than use lousy, but stable software.
     
  3. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Well yeah I know that thier joystick controls are pretty weak to say the least especailly when compared to Dark Basic even though your more programming the joystick axises and hats to different fucntions sorry but its a lot better once you know how I just hope that the VR controls will be better as I see that taking over from looking around the cockpit with a joystick hat view. And I am looking to control the camera with a Jaystick HAT /POV view and not a main axis like the X or Y or RZ or U or Z axises

    I don't mind even coding it to be honest as I found it a bit difficult to connect the camwera to the joystick first time I tried it though got my hovertant to work with my Saitek X45, and my T.Flight H.O.T.A.S X
     
  4. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    That is precisely why I use Rewired. It handles all types of flight controllers and you have each joystick element named. The Saitek X-45 and T Flight H.OT.A.S X are on the list. You can always try the free trial version of Rewired to see how well works in controlling POV HATs.

    @Steve-Tack uses Rewired with his Saitek X-45 also. I have used Rewired with my T.Flight H.OT.A.S X also. It is the only way to work with POV HATs in my opinion.

    https://www.assetstore.unity3d.com/en/#!/content/21676

    Rewired has the most popular flight controllers on the list too like Saitek X-55 Rhino, ThrustMaster Warthog, CH FighterStick and much more.

    See the huge list of supported controllers and scroll down to the middle to see the long list of flight controllers.

    http://guavaman.com/projects/rewired/docs/SupportedControllers.html
     
    Last edited: Feb 8, 2017
  5. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,237
    Yeah, if you want to support flight sticks, buy Rewired. There isn't another reasonable option that I'm aware of. Rewired comes with "templates" for gamepads and HOTAS (flight sticks and throttles). So if you want to map an action to pitch or hat switch 1, you can map that to the generic HOTAS template, and Rewired will translate that to a large number of devices. If you go ahead and map to the gamepad template too, you get support for like 90 gamepads automatically too, with little effort. As in, it just works for your players with no mapping required on their end.

    Plus if you put the Rewired remapping UI in your game, you can essentially support just about any input device, no matter how obscure it is.
     
  6. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Thank you guys I will have a look at Rewired, if it can do the Joystick POV great of course the camera view will be controlled via smartphones and the Occulus rift but for those who don't have it and can control the camera with a Joystick Hat or POV well that would be great and professional
     
  7. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    (1) Rewired has a very active forum thread and great developer support.

    https://forum.unity3d.com/threads/rewired-advanced-input-for-unity.270693

    (2) Rewired HOTAS templates

    In the Rewired HOTAS templates the HAT1 should be the POV HAT across all of the more popular joysticks.

    In my forum message post I talk about the Rewired HOTAS template subset that appears to work fine from the $20 ThrustMaster USB joystick all the way up to the high end $500 ThrustMaster Warthog. So HAT1 on the template should cover your POV for most joysticks.

    https://forum.unity3d.com/threads/rewired-advanced-input-for-unity.270693/page-56#post-2953481

    (3) Rewired Control Mapper

    This is the drop in UI (that Steve Tack was talking about) that will allow your users to remap the various controllers too.

    More info can be found here:

    http://guavaman.com/projects/rewired/docs/ControlMapper.html

    (4) This is where you can get the trial version of Rewired.

    NOTE: The only limitation is a 2-minute timeout on during runtime. Each time you start the game the timer is reset so you can evaluate as long as you like.

    http://guavaman.com/projects/rewired/trial.html
     
  8. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    This was actually a good test case so I created an example:

    Unity 5.5.0f3 (Windows 64-bit)
    Rewired 1.0.0.112

    So I used the POV cameras defined in X-plane 10 for my test case since you said cockpit. So I created a new scene with the Rewired Input Manager. Added a cube and then attached the following script (at the bottom of this message) to the cube. For mappings I mapped keyboard, Racing Wheel template, HOTAS template and gamepad templates just for completeness.

    scene_POV_example.JPG

    Rewired is all about actions so the POV camera names are names I picked for my actions. So you only have to worry creating your actions and then mapping joysticks, gamepads, wheels, keyboard and mouse to your actions.

    First we define the player and what maps the player uses:

    NOTE:
    In this case we will be assigning the default joystick and default keyboard maps to the player.

    Rewired_player_def_POV_example.JPG

    Define Actions used by the player

    NOTE:
    These are based on the X-plane POV camera names since they were a good example for a cockpit POV example.

    scene_actions_POV_example.JPG

    Map the HOTAS templates. I also mapped the Racing Wheel and Dual Analog Gamepad templates for completeness.

    scene_joystick_maps_POV_example.JPG

    Map the keyboard

    scene_keyboard_maps_POV_example.JPG

    Then we are all done and the best part is it does not matter if I press POV HAT on my joystick, button on my gamepad, button on my racing wheel, or a key on the keyboard. It all handled automatically for me in the code. So your code is very clean.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Rewired;
    5.  
    6. public class POV_test : MonoBehaviour {
    7.  
    8.     public int RewiredPlayerId = 0; // The Rewired player id of this character
    9.     private Rewired.Player Rewiredplayer; // The Rewired Player
    10.  
    11.  
    12.     void Awake()   // added 1/15/2017
    13.     {
    14.         Rewiredplayer = ReInput.players.GetPlayer(RewiredPlayerId); // added 1/15/2017
    15.     }
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.  
    25.         if ( Rewiredplayer.GetButton("Moving_Spot") )
    26.         {
    27.             Debug.Log("POV: Moving Spot");
    28.         }
    29.  
    30.         if (Rewiredplayer.GetButton("Hold_at_Location"))
    31.         {
    32.             Debug.Log("POV: Hold at Location");
    33.         }
    34.  
    35.         if (Rewiredplayer.GetButton("On_the_Runway"))
    36.         {
    37.             Debug.Log("POV: On the Runway");
    38.         }
    39.  
    40.         if (Rewiredplayer.GetButton("Circling_the_Aircraft"))
    41.         {
    42.             Debug.Log("POV: Circling the Aircraft");
    43.         }
    44.  
    45.         if (Rewiredplayer.GetButton("Tower_View"))
    46.         {
    47.             Debug.Log("POV: Tower View");
    48.         }
    49.  
    50.         if (Rewiredplayer.GetButton("Ride_Along"))
    51.         {
    52.             Debug.Log("POV: Ride Along");
    53.         }
    54.  
    55.         if (Rewiredplayer.GetButton("Track_fired_Weapon"))
    56.         {
    57.             Debug.Log("POV: Track fired Weapon");
    58.         }
    59.  
    60.         if (Rewiredplayer.GetButton("Chase"))
    61.         {
    62.             Debug.Log("POV: Chase");
    63.         }
    64.  
    65.  
    66.         if (Rewiredplayer.GetButton("Exit_POV_Test"))
    67.         {
    68.             Application.Quit();
    69.         }
    70.     }
    71. }
    72.  
    = = = = = = = = = = = =

    NOTE: The next message will have a zip file with (the files I used) more information on how I tested this.
     
    Last edited: Feb 13, 2017
  9. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    Test setup requirements: Unity 5.5.0f3 and Rewired 1.0.0.112

    This attached file contains a zip file with the Rewired Input Manager prefab, example scene, and script. So unzip the file and then drag the Rewired Input Manager into your new scene, add a cube, attach the included script to your cube and that is it.

    Testing was done using a XBOX-360 gamepad, ThrustMaster T-Flight Hotas X, keyboard, ThrustMaster T300RS racing wheel, and other HOTAS systems.
     

    Attached Files:

    Last edited: Feb 13, 2017
  10. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Thank you very much for your work longroadhwy I look forward to testing all this myself and implementing it in my game . I shall have to buy this utility but this is just what I need Great and thank you once again
     
  11. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    No problem at all.

    Everything I did also works with the trial version of Rewired too.

    Been a fan of Rewired since the beta release. Definitely the most important third party asset I own. It also has very good documentation too. Very helpful FAQs and just well organized documentation.
    http://guavaman.com/projects/rewired/docs

    The nice part about the native input that Rewired provides is that input works the same across Windows/MAC/Linux. This is a huge benefit especially for those who love Flight controllers and want to have releases on multiple platforms.

    With regards to VR both ThrustMaster and Logitech's Saitek unit sell a large number of HOTAS systems for use with VR also. The Elite Dangerous space game is very popular with both HOTAS devices and VR. It is a very good example of both HOTAS and VR working together. But also Elite Dangerous works just fine with joysticks and HOTAS systems without VR.
     
    Last edited: Feb 12, 2017
  12. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    Have you been able to integrate Rewired with your game yet? Just wondering how it was going.

    Rewired is on sale if you have not purchased yet. (Unity's wish list sale).
     
  13. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    [QNo nmot yet as I am testing the demo and having a a few problems connecting ut , must say though it has akk the axises and buttons, As my game is set on actual cyberplanets I hope I can get this to work for tanks hovr and hovercraft Tanks and Air/Space fighter craft. I9 have to wait till I can get some cash again to get the full version , hopefully I will be getting that soon ..
     
  14. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449
    Glad you are working with the trial version.

    If you have any questions you should post them in the main Rewired forum thread so more people can see your question and of course the author of Rewired can provide good guidance on what to do.

    Rewired Forum thread:
    https://forum.unity3d.com/threads/rewired-advanced-input-for-unity.270693/
     
  15. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    OK I will Thank you very much for your help
     
  16. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Yeah the problems I am having is I don'yt know which axies in Unity and Rewird are for Joy U or Z for ward and backward movement and what would be for Rudder/Straffing movement which is usually Joystick Axis RZ as well as getting the Joysticjk POV hat controls
     
  17. longroadhwy

    longroadhwy

    Joined:
    May 4, 2014
    Posts:
    1,449