Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Force feedback for Windows

Discussion in 'Made With Unity' started by Vectrex, Feb 16, 2011.

  1. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    edit:
    TEN YEARS LATER

    skaughtx0r redid this, source is on git. Thanks to everyone helping fix this.
    https://forum.unity.com/threads/force-feedback-for-windows.78268/#post-6670495

    Hi, we're working on a vehicle simulator, so one of the things we need is force feedback (eg steering wheels). The Unity input system doesn't have it, so here it is :)
    It's a PRO only feature as it's a C++ plugin which uses DirectInput.
    This is a first successful test but it needs some work. The show stopper at the moment is a crash bug. It's a weird one, where the FIRST time you run it in the Unity editor, it runs fine. You click stop, then if you click play again, Unity will crash. Odd. I presume it's some cleanup issue but I can't find it. Any help would be great.
    It's a modification of the DirectX June 2010 SDK DirectInput FF sample.

    ps Unity, can we please get script access to the input system?

    edit: If you can help go here where we're discussing what the problem might be
    http://forum.unity3d.com/threads/78580-Force-feedback-plugin?highlight=force+feedback
     

    Attached Files:

    Last edited: Jan 24, 2021
    McDev02 likes this.
  2. rahuxx

    rahuxx

    Joined:
    May 8, 2009
    Posts:
    537
  3. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    Hey, yeah I have. PM me about it. I've done chromadepth and 'normal' passive 3d. Not anaglyph or interlaced or any tricky techniques.

    BTW, about the force feedback crash, I've got a post over on support since I still need help on it.
     
  4. cemC

    cemC

    Joined:
    Dec 23, 2010
    Posts:
    214
    Good
     
  5. hyp78

    hyp78

    Joined:
    Jan 26, 2010
    Posts:
    10
    Fantastic plugin, Cameron. :)
     
  6. apiffer

    apiffer

    Joined:
    Apr 28, 2011
    Posts:
    2
    Awesome.

    Sent you a PM.
     
  7. graealex

    graealex

    Joined:
    Aug 8, 2011
    Posts:
    4
    I fixed your issue with the plugin. You did not reset the number of axis to zero before enumerating them, so at the second launch, you tried to create the effect with double the axis which are available, leading to E_INVALIDARG. The subsequent call on the uninitialized effect pointer leads to the access violation that crashes Unity.

    View attachment $FFConst.zip

    Edit: Btw, you can make a debug build of the plugin, copy that to the Assets folder and then attach the Visual Studio debugger to Unity. You can then set breakpoints in the plugin.
     
    Last edited: Aug 8, 2011
  8. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    Beautiful! I wasn't looking forward to tracking that down. I'll test it and if it works I'll stick it up on the Asset store for free, not worth charging for I think. Give me a PM with your real name and I'll put a note in the header if you'd like
     
  9. graealex

    graealex

    Joined:
    Aug 8, 2011
    Posts:
    4
    It really wasn't a big deal, at least with the debugger. Stepping through the code, I noticed the wrong number of axis at the second initialization. I also changed the cooperative mode to background, which should still work for most joysticks and wheels, and the acquisition stays when the window focus is lost. Note in the header is not necessary, but thanks.
     
  10. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Force feedback would make a great addition to the Asset Store. I'm sure many devs will appreciate it. :)
     
  11. sipon

    sipon

    Joined:
    Feb 8, 2009
    Posts:
    143
    Hi !
    Force feedback work well ! ... the first time !
    Vectrex, can you please update the zip with Graealex fix ? I don't know anything about visual studio.

    Got a 2nd question :
    - the wheel rotation is 180°, how to have the full 900° of G25/27 ?

    tks
     
  12. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    I have the exact same questions.
     
  13. graealex

    graealex

    Joined:
    Aug 8, 2011
    Posts:
    4
    I patched and recompiled the plugin:

    View attachment $UnityForceFeedback.zip

    To get the full 900° rotation with G25/G27 use the Logitech Gaming Software Profiler and use the properties of the steering wheel to set to full range.
     
  14. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    Awesome graealex! Thank you.
     
  15. graealex

    graealex

    Joined:
    Aug 8, 2011
    Posts:
    4
    If you have problems with the DLL, either download and install "Microsoft Visual C++ 2010 Redistributable Package", or download this static build that doesn't require the MSVCRT libraries.

    View attachment $UnityForceFeedback_static.zip
     
  16. fxcarl

    fxcarl

    Joined:
    Apr 8, 2010
    Posts:
    16
    move to Unity3.5 today, Test Demo Crash Editor and show this message on my pc:

    Unity Editor [version: Unity 3.5.0f5_dae8896b94af]

    UnityForceFeedback.dll caused an Access Violation (0xc0000005)
    in module UnityForceFeedback.dll at 0023:6a3412d3.

    graealex, would you like to make this plugin works on Unity 3.5 again ? ;)
     
  17. Iamdain

    Iamdain

    Joined:
    Feb 3, 2010
    Posts:
    90
    Hey guys,

    I've just tested on:
    -Windows 7 Ultimate
    -DX11
    -Unity 3.4.0f5
    -MS Visual C++ 2010
    -Logitech G27

    Crashes with the same Access violation as FXCarl received. @FXcarl ever find a solution?

    @graealex any ideas what the problem causing this is?
     
  18. milhou

    milhou

    Joined:
    Apr 5, 2012
    Posts:
    1
    Hi,
    I put the library in my project using unitycar but the force feedback only works on the unity of the game window. When I create the build the force feedback does not work.
    I'm using the InputManager unitycar.
    Anyone know if I need to do some special configuration?
     
  19. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    It's working fine on both of my steering wheels in the latest version of Unity. The only times it has ever crashed for me is when I run it without the force feedback device plugged in.

    However, I wish more parameters from the device could be exposed.
     
  20. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    ive been messing with this and wondered if anybody is still getting the bug that makes it crash every time the editor is restarted..... thanks.

    3.5.3 Pro
    Windows 7
    Ultimate 64
    DX9
    DX11

    Ive tried all three on this page.

    Thanks
    Kevin
     
  21. rogeliopuig

    rogeliopuig

    Joined:
    Dec 29, 2011
    Posts:
    13
    Good evening,
    was having the same problem here.
    Took the sources and recompiled,
    it fixed the crash in the editor when running the second time and a crash on the game when reloading a scene.
    Find the dll attached!
    Let me know if it works for you!!
    View attachment $UnityForceFeedback.rar
     
  22. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    Wow..... much thanks for this...... It fixed it. No crashes anymore.

    Now i can begin to tweak this FFB without having to restart the editor EVERY time....lol

    Thanks again.

    Kevin
     
  23. UnityExample

    UnityExample

    Joined:
    Aug 14, 2012
    Posts:
    2
    i don't get it, when i put in the dll from the first post and the script from there, all works fine, expect the crash after second start. when i try the last dll from "rogeliopuig" it doesnt work and throws an exception "dllNotFound". what i'm doing wrong?

    @devotid, how you could get it working?
     
  24. lpradalier

    lpradalier

    Joined:
    Jan 2, 2014
    Posts:
    1
    Can someone post a scene exemple to help the community ?
    I use this to import the dll...

    Code (CSharp):
    1.  
    2.     DllImport ("UnityForceFeedback");
    3.  
    How can I add, for exemple, a left rotation force feedback ?
    I am working with a thrustmaster T500RS on windows 8.1.
    Thanks.

    Jean Roukas
     
  25. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    I think what you probably do is use SetDeviceForcesXY(x,y). A negative x value should apply a force in one direction, positive in the other. See the ForceFeedbackTest.cs file in Vectrex's original post.

    Does anyone have source code for the original dll they could provide? Unity is telling me it wants a 64 bit version of it.
     
  26. p_025

    p_025

    Joined:
    Apr 6, 2013
    Posts:
    22
    Sorry to bump a really old thread like this, but I thought I'd update with my own progress.

    I've done a bit of work on the source given by Vectrex and graelex, mostly to stop the thing from crashing. It no longer crashes when there is no Force Feedback device connected, or when one of the functions is called with no Force Feedback device connected. I'm a C# developer, so I suck at C++ so I did this with null checks. I'm not sure if C++ developers are against null checks for some reason, but this was effective.

    I also got an x86_64 version compiled so it will work on either x86 or x86_64 versions of Windows. It also supports hot-plugging, but I wasn't able to get the plugin to detect when a FF device is UNplugged, so you have to manually call the ResetForceFeedback() function (or do something similar in your own implementation) in order to disable it again. But at least it won't crash.

    I'll upload my builds along with the source here. If anyone wants to grab it and run with it and add extra features, go right ahead.

    Essentially, when one of the extern functions returns an int that's less than 0, there's some sort of error. If it's 0 or greater it indicates success.

    EDIT: You know, I'm pretty sure this thing has a nasty memory leak. Like I said, I suck at C++. I'm gonna have to look into this thing a bit further.
     

    Attached Files:

    Last edited: Feb 12, 2016
    Todd-Wasson likes this.
  27. pleribus77

    pleribus77

    Joined:
    Jul 9, 2013
    Posts:
    36
    Any progress? I imported your DLL's and noted the leak as well.

    Would like to use these on a current project, but can roll back to a 32 bit build if need be (and use other FF dll I have on hand).
     
  28. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    Same thing here... Huge leak in the profiler with this. I was all excited as it wasnt crashing for a change. :)

    I did find something about the Logitech SDK though. If you dont change any settings and only send it forces... ie; Constant or spring force and not change ANY paramaters.... it will not crash each time. I am trying to find the thread that i found it in but have not been successful. When I find it i will report back here.
     
  29. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
  30. pleribus77

    pleribus77

    Joined:
    Jul 9, 2013
    Posts:
    36
    I had difficulties with Logitech SDK and a G27 wheel - worked fine on a Driving Force GT - let me know how it goes.
     
  31. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    Welllllllll.........I have got it "working" with the Logitech SDK from the Asset store and Unity 5.3.3p1

    Im using a Thrustmaster T500r with a leo bodnar box for a old G27 Sequential Shifter setup and another bodnar box running a home made handbrake. I have not tested it yet with my other G27.

    Im using Rewired Plugin from the asset store with Direct input (Ive tried them ALL and this one is AMAZING action based setup)

    Problems I have to fix yet:
    - I have to save the scene EVERYTIME and close/reopen the editor so the Logitech SDK can get the Scene Window ID (Window must be in focus in a standalone or the SDK loses connection.) Shouldnt be a problem in the game as my users have no reason to leave the game until quiting.
    - Logitech example script only gets the first FFB controller plugged in. ( not really a prob for me as users only use one steering wheel.) I have to make a check of each controller plugged in to see if it has FFB and then find the one that has FFB. Should not be too hard.
    - I cannot change ANY of the wheel parameters or the editor will insta-crash. (example: Wheel Degrees of rotation, Force Strength, Center Spring... Etc.) I will just have to make my users preset their wheel rotation to max before opening the game. I will then set those in my game with a switch to use "actual cars wheel rotation" or a chosen "shorter lock".

    Just install the sdk and then place this BAREBONES script on a ForceFeedback controller Gameobject and feed the "force" public variable. My "force" comes directly from the front wheels of my car controller. I do not use any smoothing yet but I will be adding a slider to my game for the users to choose if they want to dampen the forces.

    THe force is silently clamped by the sdk at -100 and 100 so plan accordingly.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Text;
    5.  
    6. public class LogitechSteeringWheelTEST : MonoBehaviour {
    7.  
    8.     public int force;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         Debug.Log(LogitechGSDK.LogiSteeringInitialize(false));
    13.     }
    14.  
    15.     void Update () {
    16.         //All the test functions are called on the first device plugged in(index = 0)
    17.         if(LogitechGSDK.LogiUpdate() && LogitechGSDK.LogiIsConnected(0)){
    18.        
    19.             LogitechGSDK.LogiPlayConstantForce(0, force);
    20.         }
    21.     }
    22. }
    23.  
     
  32. CISFerrol

    CISFerrol

    Joined:
    Jun 12, 2015
    Posts:
    2
    Hi,

    I'm trying to use this dll (UnityForceFeedback) but Unity is throwing a "DllNotFoundException: UnityForceFeedback
    ForceFeedBack.Update () (at Assets/ForceFeedBack.cs:46)".

    I have the "ForceFeedBack.dll" in the Assets folder but I tried putting it in the Assets/Plugins/x86 and Assets/Plugins/x64 folders with the same result...

    I have Windows 7 64-bits and Unity Pro. I would like to know if I have to install another libraries in order to use this plugin.

    Thanks in advance.
     
  33. devandart

    devandart

    Joined:
    Jun 7, 2013
    Posts:
    144
    I know, this thread and your post is old.
    But you have to recompile the DLL for x64.
    I attached the x64 version.

    Edit: But the more easier way is to use just the Logitech SDK from Asset Store. Works just fine with other devices than Logitech.
     

    Attached Files:

    Last edited: Jun 27, 2017
    Artoodiitoo and devotid like this.
  34. Artoodiitoo

    Artoodiitoo

    Joined:
    Jan 27, 2014
    Posts:
    65
    Awesome, I was just looking for 64-bit version of the ffb script for my racing game! Thanks, will try it out!
     
  35. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    Are you 100% positive it works with other devices? Because I'm trying to implement it with the Driving Force Pro which is just the previous wheel before the G-Series and I can't get the Logitech script to recognize it. Unity recognizes it as a joystick though and so does Windows. It states its for the G-Series as well so I'm not so sure it works with other devices.
     
  36. devotid

    devotid

    Joined:
    Nov 14, 2011
    Posts:
    445
    Ive used it with Thrustmaster G500, Logitech G25 G27 G29, Logitech Driving Force GT and fanatec wheels. It works with almost all that I have tried. So I would say "Most". Im not sure about the custom wheels (Leo Bodnar, Neils H etc...) Those I have not tried.

    Kevin
     
    MathewHI likes this.
  37. skaughtx0r

    skaughtx0r

    Joined:
    Mar 9, 2014
    Posts:
    74
    I've been looking into implementing Force Feedback for my game and found this post. I spent the last few days using the FFConst example and p_025's modifications to Vectrex's code as reference to basically rewrite the DirectInput Force Feedback implementation.

    It still needs a lot of work, but I got it to the point where Constant Force is working.

    I tried to make it more generic, currently it only supports one device at a time, but you can now enumerate devices and select the one you want to use. Eventually I want to add support for sending to multiple force feedback devices. I put the code up on github, I'm trying to sort out how to make a UPM package.

    Feel free to use it or contribute back: https://github.com/skaughtx0r/unity-ffb
     
    Vectrex likes this.
  38. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    Nice thanks. I haven't looked at this thread for 10 years :D Would be nice to make a plugin for the new Input System.
     
  39. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    Is there a manual or example scenes on how to use it?
     
  40. skaughtx0r

    skaughtx0r

    Joined:
    Mar 9, 2014
    Posts:
    74
    Unfortunately not, I can try and document things a bit better. In the meantime, I mainly just use it for applying constant force. I just create a GameObject in my main menu that gets set to Don'tDestroyOnLoad. That has the UnityFFB component added to it.

    Then where I calculate the FFB forces I just set:

    UnityFFB.instance.force = (int)ffbForce;

    There's also methods to:

    Enable: UnityFFB.instance.EnableForceFeedback();
    Disable: UnityFFB.instance.DisableForceFeedback();
    Start Effects: UnityFFB.instance.StartFFBEffects();
    Stop Effects: UnityFFB.instance.StopFFBEffects();
    Select Device: UnityFFB.instance.SelectDevice(string deviceGuid); // devices are listed in UnityFFB.instance.devices

    By default it auto selects the first device and adds a constant force.

    There is also now another FFB package from MrTimcakes, this was originally forked from my repo, but looks like he completely re-wrote it now. It appears to integrate more closely with the new Unity Input system and has full DirectInput support. I'm not sure if it's documented any better: https://github.com/MrTimcakes/Unity-DirectInput
     
    MathewHI likes this.