- Home /
How to connect move speed to the bobbing speed?
I have the moveSpeed variable and the head bobbing script:
private float timer = 0.0f;
float bobbingSpeed = 0.18f;
float bobbingAmount = 0.04f;
float midpoint = 0f;
public Joystick joy;
public CamRotate cam;
void Update () {
float waveslice = 0.0f;
float horizontal = joy.Horizontal;
float vertical = joy.Vertical;
Vector3 cSharpConversion = transform.localPosition;
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
timer = 0.0f;
}
else {
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2) {
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0) {
float translateChange = waveslice * bobbingAmount;
float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp (totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
cSharpConversion.y = midpoint + translateChange;
}
else {
cSharpConversion.y = midpoint;
}
transform.localPosition = cSharpConversion;
}
How can I connect moveSpeed variable to the bobbingSpeed variable, so it decreases in proportion to the decrease in moveSpeed
Comment
If the move and bob scripts are seperate, I'd create a static move speed member in the movement script, and access it with the bob script, multiplying the bob by the speed.
In move script
public static float GetCurrentSpeed()
{
return $$anonymous$$athf.InverseLerp(0, maxSpeed, currentSpeed);
}
private static float currentSpeed;
private const float maxSpeed = someNumber;
And in your bob script
public float maxBob;
private float currentBob;
void Update()
{
currentBob = maxBob * Some$$anonymous$$ovementScript.GetCurrentSpeed();
}