0
\$\begingroup\$

I am trying to figure out a good/performant way to give a piece of track multiple values of dynamic friction. A track piece is 10x10 units in Unity and the idea is to puzzle them together to a big track like the tracks in Trackmania. The game itself features no acceleration, except from gravity. To make things more interesting I want to give each track piece at the start of the scene multiple random friction values within a given range.

Currently I achieve this by making each piece of track consisting of 100 little tiles which are 1x1 units and they get a random friction values assigned. This approach works somewhat fine as long as there are less than 30 to 40 track pieces(3000-4000 tiles) but with more than that the fps are dropping really low. As it is a racing type of game I had to set the "Fixed Timestep" in the project settings to 0.001 to get accurate time measurments and this is hurting the performance as well. With a lower timestep the collision detection with all the little tiles is really bad too.

Is there a more elegant and/or performant way to achieve this in within the unity physics system?

\$\endgroup\$
4
  • \$\begingroup\$ Are you sure you already exploited all the performance tweaks the Unity physics engine has to offer? Did you mark the non-moving colliders as "static"? Did you avoid mesh colliders when possible? Did you pick the most lightweight collision detection methods the situation allows? \$\endgroup\$
    – Philipp
    Jul 6, 2020 at 9:04
  • \$\begingroup\$ They are static and how else then with a mesh colider am I able to make a colider for a mesh? A lightweight collision detection is afaik not possible, even the default collison detection was not good enough to allow a smooth rolling on the tiles. \$\endgroup\$
    – Gistiv
    Jul 6, 2020 at 9:12
  • \$\begingroup\$ A fixed time step of 0.0001 is not a practical solution. You might want to post a separate question describing in detail the problem you're trying to solve with this setting, to gather better solutions to that issue. \$\endgroup\$
    – DMGregory
    Jul 6, 2020 at 11:35
  • \$\begingroup\$ Can you show us what one of these track segments looks like, and where you want different friction values on it? Spatial/geometric questions often do better with an image example. \$\endgroup\$
    – DMGregory
    Jul 6, 2020 at 11:36

1 Answer 1

1
\$\begingroup\$

It sounds like you should use trigger colliders and a little bit of manual physics to achieve what you want. With this approach, you can place trigger volumes over portions of the track rather than having to break the track into tiny pieces.

For example, add a new GameObject with a box collider to the scene. In the box collider's Inspector, check the "Is Trigger" box. Align the box collider with a portion of the track. Create a new script and assign it to the box collider:

[RequireComponent(typeof(Collider))]
public class FrictionArea : MonoBehaviour {
    [SerializeField] private float friction = .02f;

    public float Friction => friction;
}

Then create a new component and assign it to your vehicles:

[RequireComponent(typeof(Rigidbody))]
public class FrictionApplier : MonoBehaviour {
    private Rigidbody body;
    private float friction = 0;

    void Awake() {
        body = GetComponent<Rigidbody>();
    }

    void OnTriggerEnter(Collider trigger) {
        FrictionArea fa = trigger.GetComponent<FrictionArea>();
        if (fa != null) this.friction = fa.Friction;
    }

    void FixedUpdate() {
        //you might want something more sophisticated than this
        body.velocity *= (1 - friction);
    }
}

You can put as many FrictionAreas as you want in the scene and align them with different portions of the track.

\$\endgroup\$
3
  • \$\begingroup\$ Nice Idea. I'll give it a try and evaluate the performance differnce \$\endgroup\$
    – Gistiv
    Jul 7, 2020 at 7:30
  • \$\begingroup\$ @Gistiv How did it work? \$\endgroup\$
    – Kevin
    Aug 6, 2020 at 0:58
  • \$\begingroup\$ It was more performant but I ended up using only one friction value per trackpiece as it made little to no differnce in how it felt \$\endgroup\$
    – Gistiv
    Aug 6, 2020 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .