Search Unity

Kart-Racing Physics

Discussion in 'Assets and Asset Store' started by abar, Jul 23, 2014.

  1. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Announcing a new Kart-Racing Physics simulation.

    Kart-Racing Physics provides an easily tunable arcade-style physics simulation in the vein of classic racing games like Mario-Kart.

    Try out the Web Demo to see how it plays.

    Features:
    • Simple to setup - simply swap in your own models and be ready to race in no time.
    • Easy to tweak - no complex torque curves or tire friction values, just adjust parameters like top speed, acceleration, traction, etc to create just the feel you want.
    • Built-in support for arcade-style powerups - easily give the vehicle a speed boost, make it spin around and slow down, and several other simple to use effects.
    • Fully documented C# code - clear, readable and easy to modify
    • Fully working example scene with race-track and powerups
    Available in the asset store here.
     
    Last edited: Jul 29, 2014
  2. b4c5p4c3

    b4c5p4c3

    Joined:
    Jan 4, 2013
    Posts:
    537
    Looks very nice.

    After testing this, my only observation is that in this kind of games i'm used to drive like this.

    Keep accelerator pressed while pressing left and right. When there is a curve i release the accelerator and take the curve pressing left or right while the Kart maintains some speed. After the curve keep accelerating.

    When i try to do this with your asset as soon as i release the accelerator the kart stop, so taking the curves with some speed is hard.

    I hope you can understand my poor english.

    Other than that i think is very cool.
     
    Last edited: Jul 23, 2014
  3. randomperson42

    randomperson42

    Joined:
    Jun 29, 2013
    Posts:
    974
    Yeah he's right and I would also make the demo lighter.
     
  4. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Thanks for the feedback. The deceleration speed can be easily tweaked in the editor, so you should be able to make it feel exactly how you wish.
    I agree the demo seems a bit dark, I'll tweak the lighting an upload a new one (maybe I'll change the deceleration speed a little too).
     
  5. Shigidy

    Shigidy

    Joined:
    Dec 14, 2011
    Posts:
    441
    I was so excited when I read the title and couldn't wait to play. After playing I do have some concerns before I would buy anything.

    -Is there any A.I.
    -Do the A.I. use the power-ups.
    -Needs a red shell/green shell or green rocket, red rocket Lock on and free fire missiles.
    -Power up that makes every other racer slower.
    -Power up that reverses the controls. Makes left go right and right go left.
    -Sound for background.
    -Cars seem to "jump" around if you take corners fast and skip around.
    -Does it work with mobile.
    -Do cars reset, if off-track.
    -How easy is it to change models for the karts and the power-ups.
    -Is there anyway there can be random power-up boxes on the track, like you see on mario kart.
    -Is there anyway this could be set-up with photon multiplayer.


    If all of this was done would be willing to buy for 150-200.
     
  6. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Ok, so at the moment it very much focuses on the physics of a single kart. I do have plans to build a more fully featured kart-racing template that includes things like tools for laying out track checkpoints, AI drivers, different race modes (time trial, versus etc) and so on, but those things are not there atm. What is there is a fairly robust driving physics implemented with a clear interface so adding things like AI controllers, shells etc should be possible to do in a clean and straightforward manner.

    To answer your specific feature queries:
    There is currently no driver AI
    There's no support for the visuals of targeting and firing missiles, but the physical effects of being hit are implemented (spinning around, slow downs etc)
    I like the idea of swapping left and right - I'll try and add this.
    There is no built-in engine sound fx right now, but it shouldn't be too hard to cross fade samples based on the current speed - I'll think about adding an example of this.
    I think the occasional 'jumps' are caused by my track mesh not being 100% smooth
    It should work without problem on mobile (you'll need to implement a suitable touch control scheme of course)
    Cars will slow down when they leave the track, but they aren't automatically placed back on the track.
    It's very straightforward to change the kart models. The power-up examples are simply sprites.
    Placing random power-ups around the track should be simple, but it's not part of this package.
    I'd not actually come across photon multiplayer before - it's probably outside the scope of what I'm intending with this package, but I'll give it a look anyhow.

    I realise those are probably not the answers you were hoping for, but I hope that clears up your questions at least. As I said, I will be starting work on a more complete racing template soon which should include a lot more of those sorts of features. It's good to know that people would be interested in it.
     
  7. Mementos

    Mementos

    Joined:
    Sep 25, 2012
    Posts:
    79
    Nice Kit, which price did you had in mind for it?
     
  8. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    If it gets accepted it will be $40.
     
  9. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Kart-Racing Physics is now available on the store here.
     
  10. WJ

    WJ

    Joined:
    Oct 25, 2012
    Posts:
    97
    Hi,

    I recently bought this and its cool and all but due to the lack of any suspension the karts go flying off the tiniest bumps, I shouldn't have to create an absolutely flat track for this to work, I'm building tracks more like mario kart so there will be slight bumps here and there and the car shouldn't be jumping off these, anyway to fix this?

    Another feature this is really missing in continuous play, if you hit a barrier it should spin or something and maintain forward speed (mario karts / angry birds go etc etc.) but here if I touch the barrier the kart stops and pretty much gets stuck, I have to force it to spin and mash buttons to get unstuck.
     
    Last edited: Aug 15, 2014
  11. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Yes you're right, the behaviour when the kart collides with things is not ideal. I'll try and take a look tonight to see if there's any more that can be done to improve those areas. It may involve having to bypass more of Unity's built-in physics to be able to get custom control over those things.
     
  12. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Ok, I've submitted a new version to the asset store. I've added some suspension to the physics so it should handle better on rougher ground now. I've also added a frictionless physics material to the kart, which helps it to slide off barriers more easily.

    That update should be live within the next few days hopefully, if you need it sooner just pm me.
     
  13. WJ

    WJ

    Joined:
    Oct 25, 2012
    Posts:
    97
    Thanks for the quick response, I'll download the new one and have a play.
     
  14. WJ

    WJ

    Joined:
    Oct 25, 2012
    Posts:
    97
    Hi Again,

    The suspension is working well now however your visual over-steer no longer works, the car just ends on 2 wheels now and then spins in the opposite direction, looked much better before.
     
  15. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    hmm, ok thanks, I'll take a look.
     
  16. jkadry

    jkadry

    Joined:
    Aug 28, 2013
    Posts:
    2
    Hi! Your webdemo isn't loading for me on Chrome, win8. Tried other browsers, doesn't seem to load either. Would love to try it out first before deciding to buy. Please let me if there is a way to try out the webdemo. Thanks!
     
    Last edited: Aug 25, 2014
  17. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    That's strange, I'm not having any issues loading it here, tried both Chrome and Firefox on Win8. I'll investigate some more and see if I can reproduce.
     
  18. jkadry

    jkadry

    Joined:
    Aug 28, 2013
    Posts:
    2
    Yea, site gets stuck in a 'connecting' state at deercat.host22.com/KartRacing.html and nothing seems to load. Tried a different computer but it also could not load the site.
     
  19. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Weird, it's definitely up, and I've tried pinging it from various locations around the world and it seems to be fine. I'm not much of a networking guru, but is it possible it's being blocked from your end somehow?

    I've uploaded the web-demo as a zip here: https://www.mediafire.com/?xgxe0uomwld6co7
    If you're still having problems connecting you should just be able to grab that and test it locally.
     
    Last edited: Aug 26, 2014
  20. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    @WJ
    I've just submitted a new version to the store, which fixes the visual oversteer issue, and also improves the feel of sliding around the corners (which got inadvertently altered when I added the suspension).
     
  21. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    what kind of physics are you using here? rigidbodies or custom simulation?
     
  22. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    It uses a rigid body with custom forces applied to model thrust, drag, friction etc.
     
  23. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    cool cool. could you give a screenshot of what it looks like in the inspector? i'd like to integrate this into my game, but before i buy it i need to make sure i don't need to rewrite a ton of things so your scripts can talk to mine and such.

    thanks!
     
  24. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Sure, here's the Kart Controller inspector view:

    KartInspector.png

    The Example Player Controller script is very simple, it just reads input from the controller and sends it to the Kart, like this:

    KartController kart = GetComponent<KartController>();
    kart.Thrust = Input.GetAxis("Vertical");
    kart.Steering = Input.GetAxis("Horizontal");

    There's no AI included, but an AI controller would also set these values to drive and steer the kart.

    You can also do things like spin the kart 720 degrees:
    kart.Spin();
    Or apply a speed boost or penalty:
    kart.SpeedBoost();
    kart.SpeedPenalty();
    (there are also overloads of those functions that take parameters, so you can control the strength/length of the speed boost etc).

    Hope that helps.
     
  25. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    that looks perfect. the FR-FL-RR-RL xforms are the wheel models i am assuming (and radii)? is acceleration time exactly the seconds it takes to make max speed or is there another factor?
     
  26. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Those are the wheel transforms, the actual models sit under those transforms.
    Acceleration time is basically the time from standing to max speed driving in a straight line along a flat stretch of track. It might be marginally affected by other parameters but it's pretty close.
     
  27. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    it does not use wheel collider physics does it?
     
  28. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    It uses wheel colliders for collision and suspension, but not for the any of the actual movement physics (thrust, steering, sliding etc).
     
  29. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    i just bought this, and it was remarkably easy to integrate into my game system. just a couple lines of code to change, so that was cool. so much better than full wheel colliders when trying to make a cartoony racing game.
     
  30. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    i would definitely suggest adding audio sources to the package though. i am rewritting a script from another one to match yours, but for future updates, that would save a lot of time if it were included.
     
  31. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    I'm glad you found it easy to integrate. I agree adding audio would be good, I need to find/create some samples that have a license that allows me to distribute them, if I can find some then I will definitely add some audio support.

    btw - feel free to leave a review/rating :)
     
  32. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    i just rewrote unity's car tutorial audio scripts. had to add fake gear adjustments to the kart controller, though, but you could do the same.

    one minor critique: the kart can become stuck in semi-vertical position (on a curb for example), and because the tires aren't actually providing force, there is no way to back up without modifications.
     
  33. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    ah, that sounds like a bug. atm it uses a raycast to see if the kart is touching the ground, I think maybe I should change this to use the isGrounded values from the wheel colliders. I'll do a new update with this change, in the mean time you could find the line (in KartController.cs) that reads:

    isGrounded = Physics.Raycast(frontWheelBottom, -Vector3.up, out hit, wheelRadiusFront);

    and make the raycast a bit longer - something like this:

    isGrounded = Physics.Raycast(frontWheelBottom, -Vector3.up, out hit, 2.0f * wheelRadiusFront);
     
  34. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    i am trying to add AI to this controller, but because the steering is based on -1, 1 input i am having issues driving the kart. i can convert a vector3 target to -1, 1 but then the wheels are basically swapping from left to right several times a second in Update, rather than smoothly like a user would hold down a button. what would be a better way to convert from user input to a targeting system?
     
  35. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    The way this is typically done is to have a target point that lies on the track some distance in front of the kart. How far in front should depend on how fast the AI kart is travelling, when it is going faster the distance should be larger so it has more time to adjust - a value between 5 and 15 meters is probably reasonable. You then get a vector from the kart to the target, and also the forward vector of the kart (kart.transform.forward), and project them both onto the xz plane by setting the y components to 0. You can then use Vector3.Angle to get the angle from the kart's current forward direction to the target direction. Then you can convert this to -1, 1 by dividing it by the kart's maximum steering angle (kart.maxSteerAngle) and clamping. Does that make sense?

    If the AI can get into a state where the target is behind it you may need to do something different (like turn the wheels in the opposite direction and reverse the kart until the target it in front again).
     
    mcconrad likes this.
  36. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    almost have it. the problem is Vector3.Angle seems to always be giving me positive angles, so the kart is always trying to reach target by steering right, never left:

    Code (CSharp):
    1.  
    2. private float AngleDir(Vector3 fwd, Vector3 targetDir) {
    3.         var angle = Vector3.Angle(targetDir, fwd);
    4.         angle = Mathf.Clamp(angle, -maxAngle, maxAngle);
    5.         angle /= 100.0f;
    6.         return angle;
    7.     }
    EDIT: i see Vector3.Angle will *always* return value from 0 to 180...hrmm. need a different method

    EDIT2: figured it out. AI's are working well and pathfinding now! thanks!
     
    Last edited: Sep 7, 2014
  37. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Excellent, was just about to reply. Sorry that was my mistake with Vector3.Angle, I should have suggested Mathf.Atan2 (that will give you a signed angle), but I'm glad you figured it out.
     
  38. Vagaldindo

    Vagaldindo

    Joined:
    Sep 8, 2014
    Posts:
    7
    I've really enjoyed ur demo!

    Just need update the bug about get stucked at semi-vertical.

    I may buy this sooner! Congrats!
     
    Last edited: Sep 8, 2014
  39. Vagaldindo

    Vagaldindo

    Joined:
    Sep 8, 2014
    Posts:
    7
    I've bought and gave a 5 stars rating!

    U do deserve it!
     
  40. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    any idea when the next update will be available?
     
  41. mcconrad

    mcconrad

    Joined:
    Jul 16, 2013
    Posts:
    155
    here is a simple gear shifting audio script if you want to include it in next update:
    Code (JavaScript):
    1. #pragma strict
    2. var speed: float;
    3. var gearThresh: int[];
    4. var source: AudioSource;
    5. private var kartController: KartController;
    6. private var gearMin: float = 0.0f;
    7. private var gearMax: float = 0.0f;
    8.  
    9. function Start() {
    10.     kartController = GetComponent(KartController);
    11. }
    12.  
    13. function FixedUpdate() {
    14.     speed = Mathf.Round(kartController.MPH);
    15. }
    16.  
    17. function Update() {
    18.     EngineSound();
    19. }
    20.  
    21. function EngineSound(){
    22.     for(var i = 0; i < gearThresh.length; i++){
    23.         if(gearThresh[i] > speed){
    24.             break;
    25.         }
    26.     }
    27.     if(i == 0){
    28.         gearMin = 0;
    29.     } else {
    30.         gearMin = gearThresh[i-1];
    31.     }
    32.     gearMax = gearThresh[i];
    33.     source.pitch = ((speed - gearMin)/(gearMax - gearMin)) + 1;
    34. }
     
    Last edited: Sep 16, 2014
    cl9-2 likes this.
  42. BillO

    BillO

    Joined:
    Sep 26, 2010
    Posts:
    61
    I have created a Photon multiplayer game using your Kart-Racing Physics asset. The only issue I have encountered is the rotation of the rear wheels. Currently, the rear wheels of the network player's kart rotate left and right in sync with the front wheels. I have tried to limit the rotation of the Y axis, but the rear wheels still steer left or right. I probably just need to rotate each rear wheel along the X axis, but have not figured out how to capture the forward/backward motion of the kart. Any assistance would be appreciated.
     
  43. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Ok, just so I'm clear what your setup is, I'm assuming that you're running the Kart-Racing physics only on the local player, then sending the kart's position, orientation and steering value over the network, and updating the networked players from that data. Is that correct?
    The first thing I would check is how you have the transforms set up for the wheels. If you look in the Start function of the KartController script, you'll see that I insert an extra transform above each of the front wheels. This is the transform I rotate to do the steering, while the original wheel transform is rotated to roll the wheels as the kart moves. Having them separated like this makes it easier as the two rotations don't interfere, so I would suggest doing something similar for your networked karts. As for why the rear wheels are steering, I would just take a good look at what transforms you're applying the rotation on when you update the networked models.

    You shouldn't need to send any extra data over the network for the rolling motion of the wheels, you can just calculate the distance traveled since the last frame, and use that to calculate the angle the wheels must have turned. Have a look at the bottom of the FixedUpdate function in the KartController to see how I do this.


    @mcconrad - Thanks for that, I may well include that in a future update!
     
  44. BillO

    BillO

    Joined:
    Sep 26, 2010
    Posts:
    61
    abar -- I'm sending the Kart rigidbody position and rotation (kart.rigidbody.position and kart.rigidbody.rotation). I'm also sending information for the front and back wheels (kart.wheelFL.rotation, kart.wheelFR.rotation, kart.wheelBL.rotation and kart.wheelBR.rotation). The position/rotation of the Kart and the rotation of the front wheels are working as expected. I originally tried to use just the Kart data to control the wheels but that did not work.

    I see the following in the Fixed Update:
    floatwheelRotationRear = (relVel.z/ kart.wheelRadiusBack) * Time.deltaTime * Mathf.Rad2Deg;
    kart.wheelBR.Rotate(wheelRotationRear, 0.0f, 0.0f);

    I have no idea how to get the relVel.z value. If I try to use the kart velocity everything goes awry.

    I probably need to get the direction from the current position.
     
  45. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    the relVel.z is simply the velocity of the kart along its forward direction. Multiplying it by Time.deltaTime gives the distance the kart has traveled forwards in the last frame. You should be able to calculate that value from the current and previous positions of the kart. For the networked vehicles it probably doesn't matter too much if you use the total distance traveled, instead of just the distance along the forward direction, like this:

    float wheelRotationRear = Mathf.Rad2Deg * (prevKartPos - currentKartPos).magnitude / kart.wheelRadiusBack;

    where prevKartPos is the worldspace position the kart was at on the previous frame.
     
  46. BillO

    BillO

    Joined:
    Sep 26, 2010
    Posts:
    61
    I set the prevKartPos in FixedUpdate(). The currentKartPos is the current network position. This almost worked, the rear wheel rotation was always moving forward even when the kart was in reverse. When the kart was not moving, the rear wheels had a slight forward rotation.

    I tried a totally different approach. I used "stream.SendNext(kart.Thrust);" and "stream.SendNext(kart.Steering);" This worked great. Front and rear wheels worked correctly. However, the network movement was a little choppy.

    I tried to use "kart.Thrust = Mathf.Lerp(syncStartThrust, syncEndThrust, syncTime / syncDelay);" and "kart.Steering = Mathf.Lerp(syncStartSteering, syncEndSteering, syncTime / syncDelay);" to reduce the choppy movement. This smoothed the movement but the wheels had a slight rotation even though kart was stationary.

    I will look into other syncing methods.

    Your assistance with this problem is greatly appreciated. The Kart-Racing Physics asset is great!
     
  47. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Thanks! I look forward to seeing the end result, I'd love to see a networked racing game using the Kart-Racing Physics!
     
  48. diegoadrada

    diegoadrada

    Joined:
    Nov 27, 2014
    Posts:
    59
    Abar, i really like your web demo, but before buying, I would like to know if this type of car physics could works well on an arcade game with different cars to kart-type, ie with sports type car. Thanks for your answer.
     
  49. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Well, it won't feel like a sports car, if that's what you want, it will always feel more or less like an arcadey kart type vehicle, but you could certainly use a sports car model and have it look like a sports car.
     
  50. diegoadrada

    diegoadrada

    Joined:
    Nov 27, 2014
    Posts:
    59
    Hello, thank you very much for your answer. To which I mean is, for example, in a different car type, the wheels are larger, implying that WheelColliders be larger than the kart-type, also the Top Speed. Do you think that this physics still work well and the car is not going to flip? I really really like your physics, is just what I need, but I have this fear.