Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Assets NWH Vehicle Physics

Discussion in 'Works In Progress' started by NWHCoding, Nov 30, 2017.

?

Which networking solution do you want to see implemented?

Poll closed Jan 20, 2019.
  1. UNet - Unity Networking, getting deprecated sometime in the future

    1 vote(s)
    4.8%
  2. Mirror - Community replacement for UNet (https://github.com/vis2k/Mirror)

    6 vote(s)
    28.6%
  3. Photon

    12 vote(s)
    57.1%
  4. None - I will be implementing my own solution

    2 vote(s)
    9.5%
  1. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    Ah, the problem is then likely the fact that I switched from rigidbody constraints to kinematic rigidbody for inactive vehicle and Unity does not do OnCollision callbacks when using default settings. Try going to project settings, physics section and ticking "Enable Kinematic Static Pairs".
     
  2. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    The problem you are having here can be fixed by enlarging the body object collider (cube) to extend to the wheels. Problem here is that since the rigidbody is so small and all the mass is contained in one relatively small spot inertia is also quite small making reactions to any force quite severe. Think of lever effect. Rigidbodies in Unity in general react quite badly to forces that are applied far out of the volume of the rigidbody colliders. Think of applying force to the object at a point that physically does not exist.
     
  3. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Hello, is there a way to use the ground detection script with a mesh (generated from a terrain) instead of a terrain?
    I can't use a simple unity terrain in my mobile game since it's too heavy and it kills performances.
    I currently have something like this:

    If this is not in the scripts already, I think it would be a really cool feature since most game developers targeting iOS/Android are forced not to use the unity terrains...
    Thank you
     
  4. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    Hello, there is no such support at the moment. One way to do it currently would be to split the mesh by splat texture and tag each object accordingly (tags are supported). Of course this might be problematic in case there is a lot of blending happening.
    Another way of doing it would be using texture coordinates: https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html
    This should be fairly easy to implement but would have to depend on pixel color at point. Modifying ground detection script to return that instead of the terrain texture index would probably be the way to go. If you are not in a terrible hurry I will add this to TODO and ship with the next update. If you are, send me an email.
     
  5. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    You are too kind, and I'm not in hurry at all...still many months before the release of my game...thank you for the fast reply as always.
    I forgot to reply to your answer related to my previous question (about the damage not working properly) so I'll do now.
    I made a new unity project and I imported only nwh physics, then I opened the demo scene and I started driving around.
    I had also modified the collider of the car I was driving (the Ferrari 458 one), by removing the box colliders and using the provided mesh collider, hoping to get better accuracy.
    To test the damage I started hitting the wall of the small house included in the demo scene at different speeds.
    I got a deformation at first, but not with the physics setting you suggested ("Enable Kinematic Kinematic Pairs") but with "Enable All Contact Pairs".
    The problem is that after this first, good, damage I was not able to damage the car any further. Even after fixing it by calling the fix function I could not get more damages. I thought this issue was related to the max damage setting but I don't think it's related to that since even after setting it to a very high value nothing changed.
    At this point I really don't know if it's something related to my physics settings (which by the way should be pretty standard since I tested this in a new project), my car settings (but again, even when using the default values it doesn't work) or if there is something wrong in the code itself.
    Again, no need to rush this...if you could check this out when you have time though that would be great :)
     
  6. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    Check #601 up above, I mentioned the fix there. I have yet to test it but it is almost certainly the reason for the problem since the collision code has not been touched for a few months and the sleep system and optimization has.
     
  7. Jbs_GameZone

    Jbs_GameZone

    Joined:
    Dec 14, 2018
    Posts:
    85
    Hello,

    I have an issue with the cars jittering at very slow speed (barely moving speed). Spring force and center of gravity are ok. From what I've seen it is related it the Side (lateral) Force Coefficient of the Wheel Controllers. To replicate this play Island scene with the Sedan and increase the Side Force Coefficient (1.6 or 1.8 for example, the higher you go the more violent the car shakes near to standing still speed, but once you get to speed everything is smooth).
    My cars are set up with longitudinal and lateral slip/force coefficients of 0.7/1.2 front and 1.2/1.6 rear (understeer bias) and they handle great in the corners at normal or high speed, they have exactly the level of grip I was looking for, but the drawback is that I have this jittering when barely moving which causes tyre screech and also shakes the camera.
    Is there a (nice) solution or workaround for this ?

    Thanks
     
    Last edited: Apr 20, 2019
  8. fsmerschkoetter

    fsmerschkoetter

    Joined:
    Apr 15, 2019
    Posts:
    1
    Hello,

    in the demo szene (manual transmission), although i got the box "automatic clutch" unchecked i am able to go freely through the gears without pressing the actual clutch. In addition the engine should be stalled if breaking to hard without hitting the clutch and it shouldn`t be possible to start in 6th gear. Is this a bad on my part with the settings, or is this behaviour just not implementet yet?

    Thank you!
     
  9. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Not 100% sure, but try to play a bit with the fixed timestep (Edit->Project settings->Time->Fixed Timestep) and check if when you decrease it it gets getter
     
  10. Jbs_GameZone

    Jbs_GameZone

    Joined:
    Dec 14, 2018
    Posts:
    85
    Out of the question, I'm targeting a large range of mobile devices (up to 6 years old) so further reducing the fixedtimestamp is not a viable option, it comes with a high CPU impact, 0.015 is extremely low as it is.
    Hacks can be done, nonetheless, I can adjust the coefficients based on the speed of the car so it will grip better the faster it goes, but this implies further checks on every update (or even worse on every fixedupdate) and I'd like to avoid that as well.
     
  11. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    Hello there and sorry for taking some time to answer, went away for the weekend.
    If anyhow possible I would recommend against upping the force coefficients too much on the wheels. The reason for the problem is that coefficient of 1 would give you about realistic grip and 1.6 would be 1.6 times that. Time passed between two physics updates is quite large (compared to real world where it is essentially 0 if you look at it in that way) and so if the calculation is too tight a kind of feedback loop gets introduced which results in jitter. It tries to compensate for error but it overcompensates and insteads moves the wheel the other way instead of just stopping it. This is precisely the reason we can not have really fixed physics joints in games. Now, there are two ways to improve this: reduce time between updates as you increase the coefficient (Time.fixedDeltaTime should be roughly 0.02/coefficient from my experience) or reduce the force coefficient - or find the compromise between the two. If you need more stability in corners and less slip you can play with stability drive assist which is not physically accurate and does not work on slip-based calculations so you can up it quite a lot. Hopefully this helps and puts some light on things.
     
    Jbs_GameZone likes this.
  12. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    It for sure would be a hack but since WheelController3D has quite a stable API you could do an external script that sets the side slip coefficient to some lerped value depending on speed:

    float sideSlipCoefficient = Mathf.Lerp(1f, 1.6f, vc.ForwardVelocity * 0.25f);


    I know it is a hack but should work fairly well. The example above lerps between slip of 1 and 1.6 from 0m/s to 4m/s.
     
  13. Jbs_GameZone

    Jbs_GameZone

    Joined:
    Dec 14, 2018
    Posts:
    85
    Yeah, most likely I will go this way, just wanted your thoughts on it as well. Thanks
     
  14. Jbs_GameZone

    Jbs_GameZone

    Joined:
    Dec 14, 2018
    Posts:
    85

    I've cracked it, not perfect, but it reduced the jittering by so much I could leave force coefficients to 1.4 and also double the FixedTimestamp to 0.03 which made a HUGE difference in the game performance. The overall handling I'd say it is ~90% sharp as before, so it won't be noticed in most games.

    It tries to compensate for error but it overcompensates and insteads moves the wheel the other way instead of just stopping it.

    This was of great help for debugging it. This corner case meant that at each iteration/computation the forces would have different signs (positive / negative), and they would be quite big in value and almost equal in absolute value. This effect can be resolved by keeping the previous computed force and always assign the mean between the previous and the current one. The minor downside is that it automatically lerps the force when it is constantly in a direction. For example instead of putting down an instant force of 5000 it will put 2500, followed by 3750, 4375, 4687 ... But it happens very fast, so it gets to >90% of the original force in 5 iterations, that is 0.15 of a second.

    Below is the updated code from WheelController.FrictionUpdate() :

    sFriction.force = (prevSForce + (Mathf.Sign(sFriction.slip) * activeFrictionPreset.Curve.Evaluate(Mathf.Abs(sFriction.slip)) * wheel.tireLoad * sFriction.forceCoefficient * 1.3f)) / 2;
    prevSForce = sFriction.force;


    Hope this helps others targeting low end devices (observation : fixedframerate is autocorrected by VehicleController.Start() method to a max of 0.015, so you'd need to comment out that part as well to get it higher).

    Cheers,
    Jbs
     
    Last edited: Apr 22, 2019
  15. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    I have actually done this and even tried doing moving average over multiple frames but beware that you might get the infamous drifting on a sloped surface when standing still effect. Of course with the newer versions of NVP it is not such a problem since it disables the vehicle when it detects it is still and makes it kinematic both to save on performance and prevent creep, but this is the reason I removed the smoothing and went for accurate instead. There is always a downside. Interpolation, extrapolation, whatnot there is always so corner case. Anyways, glad you got it there - I am thinking of doing some kind of slider to adjust this.
     
  16. Jbs_GameZone

    Jbs_GameZone

    Joined:
    Dec 14, 2018
    Posts:
    85
    Yes, the sliding slope test at standstill or near-standstill speed was the first I tried after the change but it held the car very well. And like you pointed, every change has pros and cons, and is about finding the best compromise for your scenario.

    I am thinking of doing some kind of slider to adjust this
    That would be cool
     
  17. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Hello @NWHCoding , I am trying to change the suspensions' height of my vehicle at runtime using a slider.
    The slider goes from 0 to 0.1f, and I made a function to set the y coord of the WheelController's transform component whenever the slider value changes.
    The problem is that it doesn't work...well it does something, but it's crazy: the transform component is set to weird values, and even the x and z coords are affected (when they should not be, according to my script below).
    I believe it's something related to other NWH scripts conflicting with it.
    Here is the code I used:

    Code (CSharp):
    1.  public void UpdateSuspensionsHeight(float newY)
    2.     {
    3.         Vector3 pos = wheelControllers.transform.position;
    4.         pos.y = newY;
    5.         wheelControllers.transform.position = pos;
    6.     }
    Do you have any clue about it? Am I using a wrong approach? Thanks
     
    Last edited: Apr 24, 2019
  18. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    If your goal is to raise/lower suspension while keeping the same spring length your approach is valid BUT you have a big gleaming error in there. You are settings transform.position on a child object which means that if your car is at 200 meters above origin and you set the Y of one of it's children, that child will end up 200 meters below the car.
    What you need to do instead is:

    Code (CSharp):
    1. void Awake()
    2. {
    3.      initialPosition = wheelController.transform.localPosition.y
    4. }
    5.  
    6. void SetHeight(float offset)
    7. {
    8.      wheelController.transform.localPosition.y = initialPosition + offset;
    9. }
    10.  
    Or alternatively you could just increase the spring length - depends on what your goal is.
     
    farfadet46 likes this.
  19. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Thank you, worked like a charm :)
     
  20. mthawley

    mthawley

    Joined:
    Sep 7, 2018
    Posts:
    18
    Hi. Should there be a Backfire component in the Vehicle Controller? I see references to various Backfire scripts but I don't see any input fields in Vehicle Controller. Cheers.
     
  21. NWHCoding

    NWHCoding

    Joined:
    Jul 12, 2012
    Posts:
    685
    I am pretty sure it is under Effects as it is pretty much only a visual effect.
     
  22. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Hello @NWHCoding , today I am here again to suggest you a feature that in my opinion would be really cool and useful.
    So one of the biggest issues when an asset is open source (which I actually appreciate) is that to add custom features you are usually forced to edit the asset's code itself (or write a wrapper around it that most of the times is overcomplicated and performances-expensive); but then every time an update comes you lose time trying to reapply your changes.
    How could this be mitigated? Let me explain by a few examples.
    1) I wanted to show a short message whenever the vehicle that the player was driving got flipped over. To do this i had to add a few lines of code near the flip over script. Easy, but not maintenable on the long term.
    2) I wanted to show a message whenever the player's vehicle started drifting (and count the drift time when it finished to reward the player with some coins). A bit less easy than the previous example but still nothing hard, yet again it will be broken by the next NWH update (which will be very welcomed in any case :D).

    My advice is adding some "InteractionEvents" to fit our most common needs. A new tab could be made in the vehicle controller script (Maybe called "Events"?) containing all the events that could be triggered while playing. This would provide a ui button-like GUI where we could pass our scripts' functions to it. To make this even more useful, dynamic parameters could be passed (https://forum.unity.com/threads/invoking-unityevent-with-argument.282670/).
    So, for example, the hypotetic OnDriftStart() event could pass the start time to my method...or even better, it could pass a custom struct containing all the drift info (start time, start coords and so on).
    Another event that I can think of could be "OnGearChange" to update my digital gear display only when I actually need to update it instead than having to write it every time in an Update() loop (Although there may be already a built-in method that works better for this but i have not checked yet).

    Anyway, every time I wrote here in the past asking about ways to do something, you always added what I needed to the to-do list.
    By writing this post I am really not trying to force you to add this feature, but I would love to hear your opinion about it since I really believe it could improve the flexibility of this great asset.

    Thanks for reading :)!

    EDIT: Adding a screenshow below about what I explained before just to make it more clear and easier to understand, especially for other NWH customers.

    Maybe the readers of this thread could tell us if they would find it useful or not as well
     
    Last edited: Apr 26, 2019
  23. BlandonDu

    BlandonDu

    Joined:
    Dec 26, 2016
    Posts:
    9
    I have a racing game in progress,after several days working on the integration of this driving system, it looks good until when i run the game in the real environment which is many NPC cars driving together, I found that the FPS dropped into a unacceptable number, I looked into the code, found that the fixed time step is set to 0.015, that is obviously quite a burden. 0.05 is the number i d been using considering my game is pretty heavy on physics. now, i do not have any idea to solve this problem.

    note, only the current player is using this system, other cars are still implementated with unity wheel colliders. is there anyway that i can still use a high fixed time step that NVP can live with???
     
  24. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    393
    Hi,
    Can you please AsmDef files to the asset to improve compilation time? Many assets are AsmDef friendly these days and this asset should be also.

    This will require reorganizing folder structures by putting all editor related scripts into the single Editor folder.
    Thanks.
     
    paoloarnolfo00 likes this.
  25. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,237
    Just posted the same question in the Edy's Vehicle Physics thread. I don't own NWH yet, so I'm not super familiar with it.

    Would it be possible with NWH Vehicle Physics to set up a weird suspension that would work in this sci-fi vehicle (from the asset store):


    That asset comes with a simple physics setup with a suspension that just moves the wheels with terrain. The elaborately modeled suspension pieces are then static. Ideally the lower pieces that attach to two wheels each would rotate and the big upper suspension bar things would react at least somewhat to terrain.

    If that's not a good fit for NWH, is there another asset that is more geared toward that sort of thing?
     
    iichiversii likes this.
  26. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,334
    Mirror - Community replacement for UNet (https://github.com/vis2k/Mirror)

    +1 for that should have at the top of the poll really, really good plugins for it aswel like litenetlib for udp support.

    And for the next poll.. a motorbike controller :)
     
  27. Kvazimado

    Kvazimado

    Joined:
    Oct 27, 2018
    Posts:
    5
    Hey @NWHCoding
    How can you limit the speed of the car without changing the parameters of the engine and transmission?
     
  28. GearheadLydia

    GearheadLydia

    Joined:
    May 31, 2018
    Posts:
    3
    Hello again,

    I think I may have found a bug. The motor makes the wheels spin when the car is upside down even in neutral. When in a gear other than neutral and the car is upside down, RPM hangs where it's at and won't decrease.

    (Thanks in advance if you end up looking into this.)

    @Kvazimado Try General -> Speed Limit? It's in meters per second, not kilometers per hour, in case that's an issue.
     
    Last edited: May 17, 2019 at 4:47 AM
    Bzuco likes this.
  29. Bzuco

    Bzuco

    Joined:
    Sep 6, 2015
    Posts:
    30
    Hi, I have added " * wheel.brakeCoefficient" on line 152 and 164 in brakes.cs, because I was not able to separate overall brake force and handbrake force.
    Now if I set max brake torque to 40000, axles brake coeff. 0.1 and handbrake coeff. 1.0 the car behavior looks correct when braking/handbraking.
    Also decreasing side friction force coeff. for wheels was necessary.
    If there is other solution or there was some other intention why was wheel.brakeCoefficient missing on those lines, let me know.
     
  30. Nihil688

    Nihil688

    Joined:
    Mar 12, 2013
    Posts:
    374
    I'm considering buying but I have a couple of questions:
    1) Can I use the physic calculations of the plugin without using physics? What I am trying to do is have the dial
    up and running and connect it to the speed of a model and the rotation of the wheels without having colliders
    and other physics calculations which could potentially be heavy for mobile.
    2) Does your plugin support motorbikes? I've only seen vehicles with more than 2 wheels so I wondered if you support
    less.
     
  31. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,334
    +1 would agree , more modular and easier to extend without needing to making big changes to the original source to add in hook in events.

    Also @NWHCoding some things are a little broken in 2019.1f1, the car prefabs have problem when clicked on from project window and shown in the inspector like something is changing them constantly causing lag, icon changes, mouse cursor pointer>timewait etc

    Would be nice to see the new unity 2019 input manager support for the vehicle controller, so a defined vehicle controller inputmap.

    Also wish you had supported mirror first not photon, its open source and has some really good third party assets for it that I'd rather use like https://vis2k.github.io/Mirror/Transports/Multiplexer.html and https://vis2k.github.io/Mirror/Transports/LiteNetLib4Mirror
     
    paoloarnolfo00 likes this.
  32. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Thanks for the +1, I really believe we all need something like that.
    About that 2019 prefabs window, I thought I was the only one with that issue...well, apparently not.
    A fix is required for sure since it's really annoying ^^
     
  33. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,334
    I actually first saw a similar prefab issue with another addon I'm testing for emotitron(mirror plugin dev) and he has a prefab example that was doing a similar thing in 2019.1 ..commenting out the below code fixed it, so not sure what is in the prefabs nwh-vehicle but I'd imagine maybe something similar

     
  34. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    Hi again...well after wasting so much time every single time I wanted to edit a prefab containing a vehicleController script I decided to debug that issue (And I should have done it much earlier).

    FIX BELOW:
    Open the VehicleControllerInspector.cs script, then go to line 74:
    Code (CSharp):
    1. EditorUtility.SetDirty(vehicleController);
    This is forcing unity to save the whole script variables over and over again, since it's called in OnInspectorGUI().
    To fix that, just add an if check to save those values only when we actually change some of them:
    Code (CSharp):
    1. if (GUI.changed)
    2. {
    3.     EditorUtility.SetDirty(vehicleController);
    4. }
    Disclaimer: I have never coded a unity asset nor used these kind of functions, and this fix comes from a 5-minutes google search, but as far as I know it works fine and there are no downsides. Enjoy :)
     
    Player7 likes this.
  35. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,334
  36. paoloarnolfo00

    paoloarnolfo00

    Joined:
    Mar 1, 2018
    Posts:
    24
    I saw him replying on some threads related to other assets he is working on, I guess he is focused on those one atm...
     
  37. Kvazimado

    Kvazimado

    Joined:
    Oct 27, 2018
    Posts:
    5
    @GearheadLydia thx.
    Maybe you know and about that - As in the speed limit to make the transmission used all of the transmission without changing the engine power and rpm?
    @NWHCoding