- Home /
Player's speed after collision problem
Hi! I have a player and a Berry prefab. If I collise with the Berry, the player's speed will double for 1s and progressively slow down and the player will get back it's regural speed. I used coroutine and my code works well (I think XD). But I have a problem with touching berry prefabs one after another... The actual and final speed is written out, and I get really strange results... If the player touches the 1st berry and 1s later the 2nd berry, the Final speed is 4 instead of 5. I get strange result... Can you help me to find where the problem is?
public class PlayerController : MonoBehaviour {
private float runSpeed;
private float jumpSpeed;
private float moveX, jumpY;
//Definition of objects with triggers
public Collider2D berry;
private void Start()
{
runSpeed = 5f;
jumpSpeed = 6.5f;
}
void FixedUpdate()
{
//*Time.deltaTime means: we want to move this object x meter/second instead of x meter/frame
moveX = Input.GetAxis("Horizontal") * Time.deltaTime * runSpeed;
jumpY = Input.GetAxis("Jump") * Time.deltaTime * jumpSpeed;
transform.Translate(moveX, jumpY, 0f);
}
//if player touches the small berry it will be destroyed
//the speed of the player will be boosted for X seconds
void OnTriggerEnter2D(Collider2D berry)
{
Debug.Log("The berry is touched");
Destroy(berry.gameObject);
StartCoroutine(DoubleTheSpeed());
}
//process for boosting the speed
//double speed for 1s than slow down
IEnumerator DoubleTheSpeed()
{
runSpeed = 2 * runSpeed;
yield return new WaitForSeconds(1);
// slowing down
while (!(runSpeed <= 5))
{
yield return new WaitForSeconds(0.4f);
runSpeed -= 1;
print("Actual speed : " + runSpeed.ToString());
yield return null;
}
print("Final speed : " + runSpeed.ToString());
}
}
And here are the results I got:
I get a glimpse of what's going on. But I don't really get what you want to actually happen. Please elaborate further, be more specific.
Right now anytime you collide with a berry, your player speed doubles whatever its speed currently is. After 1s speed will be checked and if it's greater than 5f (dang man! isn't runSpeed>5f much more readable than !(runSpeed
I can think of a couple reasons why this might not be the behavior your intended it to be. But I'd need you to be more specific about it: Should the speed doubling be cumulative when you pick more than one berry? Should the speed be reduced always at the same pace no matter how many berries have been picked? If that's so, right now for every berry you pick, the speed will double cumulatively, but also the speed will be then reduced cumulatively, so long as after the allowed 1s the speed is still over 5. There's also room for possibly unintended behaviors as the first speed reduction will be carried out after 0.4s, at which point, other "concurrent" coroutine executions might have already reduced the speed below the check threshold.
Answer by mlnczk · Nov 19, 2018 at 01:27 PM
Well you can workaround it with boolean. Set bool "isBoosted" and check in Update if isn't boosted then set runspeed = 5.0f. Propably not the best way but should work. Cheers!