I wasn´t sure where to put this tread so feel free to move it.
I have a truck with a working suspension. the suspension arm works great but i’d like to add the spring animation. I use 3Ds Max. altouh it has a dynamic spring wich you can use in it, it can’t be exported over to unity.
So i was wondering… how could you animate a spring in unity? I don’t need it to be functionally. just to follow the anchor point by scaling up or down.
Thx.
LW.
If you know the height of the spring, this is quite straightforward. Firstly, record the initial (local) position of the anchor point in a variable:-
function Start() {
var startPos = spring.transform.position;
}
Then, in the Update function, keep sampling the distance of the anchor from its initial position in the Y direction (assuming the spring stretches along that axis). Subtract that distance from the total length of the spring to get the current length. Then, divide the current length by the initial length to get the fraction of compression/extension. This fraction is the scale value for the transform:-
function Update() {
var dist = spring.transform.position.y - startPos.y;
var compFrac = (origLength - dist) / origLength;
transform.localScale.y = compFrac;
}