Stupid question but how do you set aside a variable?
Someone please help a newbie out. How do I set aside the value of leanVelocity so it doesn't get affected by forwardVelocity.
Background to my game: The player object "automatically" accelerates after the player inputs a direction to start running to. The player is capable of changing directions. My target "direction change event" goes as follows:
Player input change direction
Player object's current speed gets deducted by breakForce
The player object decelerates speed frame by frame until it reaches the difference of forwardVelocity - breakForce.(or maybe until it halts)
(I still have to add this) The player object pauses for half a second before accelerating again
player object continues to accelerate in the new direction. "starting" at the leanVelocity.
everytime forwardVelocity changes it affects leanVelocity. I thought using a coroutine will remedy this.
Update()
{
...
if (player_State == "player_Active")
{
forwardVelocity += accelRate * Time.deltaTime;
forwardVelocity = Mathf.Min(forwardVelocity, maxSpd);
if (player_Direction == "Left")
{
playerRb.velocity = Vector2.right * forwardVelocity;
Debug.Log(forwardVelocity);
}
else if
...
}
void FixedUpdate()
{
if (player_State == "player_Slowing")
{
StartCoroutine("Decel");
Invoke("SetActiveState",2);
}
}
IEnumerator Decel()
{
leanVelocity = forwardVelocity - breakForce;
while (forwardVelocity > leanVelocity)
{
forwardVelocity += decelRate * Time.deltaTime;
forwardVelocity = Mathf.Max(forwardVelocity, 0);
yield return null;
}
}
I have to change the whiles condition to ... > 9 for it to "work". 11 is my breakforce, top speed of 20, accelRate of 10 sec, decelRate of 2(?) not sure about this one since I have 2 more sec on Invoke
Sorry in advance if this is noobish :( Please, just point me in the direction on how to reach my goal. Thank you.
whoops! sorry for the dupe, I had to login thought I'd have to rewrite the whole thing.
Your answer