How to change suspension settings c#

Hello, I’m working on a 2D racing game. I need to be able to upgrade the suspension (which changes damping ratio and frequency). However, I haven’t been able to find out how to make this work in the game.

Here is what I have. But again, it has no effect on the car in the game. (if I change the settings in the editor, it of course changes it in the game but I need to be able to change it in code)

public WheelJoint2D motorWheel;
public JointSuspension2D jointSuspension;

void Start () {
jointSuspension = motorWheel.suspension;
jointSuspension.dampingRatio = 0.01f;
jointSuspension.frequency = 0;

}

Thanks for any suggestions!

JointSuspension2D is a struct. Structs are passed by value, not by reference like a class.

So what you’re doing is copying the values from “motorWheel.suspension” into your own local struct, not getting a reference to the suspension.

You’re changing values in your local struct, and then you need to copy that new data back into “motorWheel.suspension”.

Add this line at the end of your changes:
motorWheel.suspension = jointSuspension;

3 Likes

Wow, thank you so much. I appreciate you taking the time to help me.