MildlyDisturbing

LD 42

Ludum Dare 47

OUROBROS

Well I'm not sure if i will be able to go much farther than this, but, it was really fun as it was.

https://twitter.com/sullysaysyes/status/1312955992892674048

Click above to see the video of it thus far, i know exactly where to go next.. Maybe i'll get some done tomorrow, hopefully!

https://gfycat.com/welllitvaliddeviltasmanian

ourobros.PNG

Ludum Dare 49

Building a Simple & Fun Drifty Car Controller!

For me and my brother's game: Driving Blind

ezgif-2-60f5a828ad2d.gif

We set out to build a fun arcade-y but chunky physical little car controller, and I was really pleased with the result given how little effort it took to produce, so I thought why not share it with the rest of the wonderful people that participated in this little jam!

The premise is really simple, take any car model, I modeled one in Blender:

D4r9H5MWAAAeqKS.png

But a great starting point if you don't want to model, and also happens to be where I got the rest of the assets from is Kenney's Incredible & Free Kits

Throw that stuff into whatever game engine you are using, I'm using Unity it already has many of the physics functions you'll need for this:

cap2.PNG

Try to setup your asset hierarchy so the VISUALS are seperated from the FUNCTIONS, that way it will be easy to swap out cars down the line:

cap4.PNG

Add a Rigidbody to the top most parent (The Functional Parent):

cap5.PNG

And add a Convex mesh collider to the Visual Car (Babby Car)

Cap6.PNG

as you may have noticed, I also modelled some wheels seperately, and I slapped some materials on it, but this isn't an art tutorial, you can do all the same stuff with a box

Next up, add some transforms to where all the wheels should be, and then put ur wheel visuals under that transform. This will be the point of origin from where we will be firing some Spherecast to hit the ground. Make sure the wheels don't have colliders, they're purely visual and anything blocking the cast will cause issues with ground detection

cap7.PNG

cap3.PNG

Once you have all your wheel points placed on your car, if you press play it should just drop to the ground and flop around and do nothing, but it shouldn't fall through the ground.

T6mUYcWokP.gif

Now the Meat & Potatoes of this system! Getting this car to hover off the ground using nothing but physics!

The premise is simple, we're going to SphereCast down to the ground, and find out how far away we are, and based on that distance, we'll apply more or less force to the point at which the wheel resides. This will cause our car to hover off the ground if we have our variables set up properly:

Make a Script called "Wheel" and add this code to it:

``` using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Wheel : MonoBehaviour { public Rigidbody mainRigid; public float WheelForce; public float WheelTorque = 10; public Transform WheelTransform; public Transform WheelVisual; public float HoverDistance; public float FloatDistance; public LayerMask groundMask; public float castSize = 0.3f; public Vector3 castDisplacement = Vector3.up; public ParticleSystem GroundedParticles;

public bool grounded = false;

void Awake()
{
    WheelTransform.SetParent(null);
}

void FixedUpdate()
{
    //This is intended to push the Car up using physics at the position this transform lies.

    Ray hitRay = new Ray(transform.position + castDisplacement, Vector3.down);

    RaycastHit hitInfo;
    if (Physics.SphereCast(hitRay, castSize, out hitInfo, HoverDistance, groundMask))
    {
        float distanceMod = 1.0f - Mathf.Clamp(hitInfo.distance / FloatDistance, 0.0f, 1.0f);
        mainRigid.AddForceAtPosition(Vector3.up * WheelForce * distanceMod, transform.position,
            ForceMode.VelocityChange);
    }
}

void LateUpdate()
{
    //This is mostly for positioning the wheels where they need to be, seems a bit redundant as it's pretty much the same as above.  
    //Was having issues removing some of the redundancy as the above needs to happen, and for this object to move, before the late update can cast again
    //And align the wheels to the ground properly.

    Ray hitRay = new Ray(transform.position + castDisplacement, Vector3.down);

    RaycastHit hitInfo;
    if (Physics.SphereCast(hitRay, castSize, out hitInfo, HoverDistance, groundMask))
    {
        if (hitInfo.distance < FloatDistance)
        {
            WheelTransform.position = hitInfo.point + hitInfo.normal * WheelTransform.lossyScale.y * 2.5f;
            grounded = true;
        }
        else
        {
            WheelTransform.position = transform.position;
            grounded = false;
        }
    }
    else
    {
        WheelTransform.position = transform.position;
        grounded = false;
    }
    WheelTransform.rotation = transform.rotation;

    if (grounded)
    {
        if(!GroundedParticles.isPlaying)GroundedParticles.Play();
    }
    else
    {
        if (GroundedParticles.isPlaying) GroundedParticles.Stop();
    }
}

public void SpinWheel(float direction)
{
    //Visual code to rotate the wheel that the car controller uses
    WheelVisual.transform.Rotate(Vector3.up*direction,Space.Self);
}

} ```

Look at the code comments to understand why there is Fixed and Late Update being used. The basic idea is that Fixed is used to enact physics on the cars body, and the Late is used to position the wheel transforms after the fact.

Note: This code is optimized for speed of me writing it, not optimization or code elegance, feel free to improve upon it

After you've written that, slap this component onto all the wheels:

wheelcomponent.PNG

NOTE: You may notice within the Wheel component inspector, there is a "Wheel Transform" and "Wheel Visual". The Wheel transform is actually going to be the one snapping to the ground,but the visual will be the one spinning. You need both so you can rotate the wheels left and right on the Y axis locally, while allowing the visual wheel to spin freely.

If you use my settings for the Car rigidbody, and the wheel components, your car should now balance when you press play:

mYgpDpf6SY.gif

Isn't that just the bees knees! Honestly, the rest of it could kinda go without being said, but all you need to do is loop through any number of wheels, for me, I chose the front wheels, and add force going relative to the Wheel forward:

``` Vector2 movementAxis = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

foreach (Wheel w in FrontWheels) { //Handle Rotation of the wheel based on player intent w.transform.localRotation = Quaternion.Euler(0, 45 * movementAxis.x, 90);

//If the wheel is grounded, let's add force to the Car rigidbody at the position the front wheels are, in the direction the wheel is pointing!
if (w.grounded)
{
    rgbd.AddForceAtPosition(w.transform.forward * w.WheelTorque /*Add (* movementAxis.y) here if you want to control forward and back as well! */, w.transform.position,ForceMode.VelocityChange);
}

} ```

That should do it!

DRIVIN.gif

You should be able to drive your car around now. It's not an entirely full proof solution, a lot of terrain it is not great at doing, but I will be improving on this foundation to have it be able to handle things a lot better.

Hope some of that was helpful! If it was, follow me and my brother for more cool little things like this in the future!

Try Driving Blind !