I am trying (and failing) to apply a force smoothing instead of just a big jump. However, it is not cooperating! The math just isn’t working out. Any help would be greatly appreciated (I have made it all the way through to weapon hitbox at this point, just going back and doing some tweaking) :
The theory:
Apply the force over a variable amount of time (lets say, 0.4 seconds)
Apply 10 force over 0.4 seconds
This sounds easy!
No, not easy …
The code:
//---------------- FROM BASE STATE
protected void MoveWithForces(float deltaTime)
{
MoveWithForces(Vector3.zero, deltaTime);
}
protected void MoveWithForces(Vector3 motion, float deltaTime)
{
stateMachine.CharacterController.Move((motion + stateMachine.ForceReceiver.Movement) * Time.deltaTime);
}
//------------------- FROM ATTACK STATE
public override void Tick(float deltaTime)
{
MoveWithForces(deltaTime);
FaceTarget();
float normalizedTime = GetNormalizedTimeThroughAnimation();
if (attack.HasForce && normalizedTime >= attack.ForceTime)
{
TryApplyForce();
}
if (normalizedTime > 1)
{
if (stateMachine.TargetingController.CurrentTarget != null)
{
stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
}
else
{
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
}
}
previousFrameTime = normalizedTime;
}
private float startTime;
private void TryApplyForce()
{
if (hasFinishedApplyingForce) return;
if (!hasAppliedForce)
startTime = Time.time;
//Apply the force over a set amount of time
float overTime = attack.ForceSmoothing;
float forceToApply = attack.Force;
float forceSteps = (forceToApply / overTime);
Debug.Log(forceSteps);
if (Time.time - startTime > overTime)
{
Debug.Log("Time!");
hasFinishedApplyingForce = true;
return;
}
//When we call the move with forces, this force is multiplied by time.deltaTime
stateMachine.ForceReceiver.AddForce(stateMachine.transform.forward * forceSteps);
hasAppliedForce = true;
}
The thought process (whining included):
When you go to apply the force for the first time, capture the start time.
If our Time.time - start time < total time, add some force
How much force? well, the (force/time) to add … right? RIGHT?! wrong… so very very wrong… in all ways wrong
the theory is, if I want to add 15 force over 2 seconds, I should be adding 7.5 force per second * time.deltaTime (this is done in the Move method on the base state) to account frame time instead of actual seconds
So, we run it through, saying add 10 force over the course of 0.4 seconds, and ZOOM the player takes off like a rocketship …
So we be stubborn and think math is solid, and change it to 0.5 force over 0.4 seconds, and it feels and looks betters … BUT, we run the maths, and find out that it ran 87 times, each time applying 0.125 force… mathy no add up, that means we added 10.875 total? and its just a teeeeny little step (not the same distance as when we didnt do all fancy math and just plopped 10 in there ) …
We learn that math no good, bad at math, beg for helps … (note: even though this feels good there is obviously some fundamental lack of understanding that is bothering me. I can keep it like this and work around it, but then I can’t say to my designer “yeah, itll add X force over Y seconds” because… it doesnt. I would just have to say “play with it till it feels good!” which is not great).