- Home /
Creating a bouncing game without physics - Question on Mathf.Sin()
So I'm creating a simple bounce the ball game where the objective is for the player to catch a falling ball, and then have that ball bounce when caught to another point and so on and so forth.
To accomplish this without using physics I'm doing a combination of Lerping (to move on the x-axis) and Mathf.Sin to accomplish the bouncing motion. In the following code below:
Vector3 startPos = transform.position;
Vector3 endPos;
endPos = new Vector3 (nextXSpace, -1.52f, 0f);
float scaledElapsedTime = (Time.time - timeStamp) / (bounceTime / speedCo);
Vector3 currentPos = Vector3.LerpUnclamped(startPos, endPos, scaledElapsedTime);
currentPos.y += bendMod * Mathf.Sin (scaledElapsedTime * Mathf.PI);
transform.position = currentPos;
There are three types of balls that can be bounced in this game: light, medium and heavy. bendMod is a modifier to Mathf.Sin that will essentially make the ball bounce higher. The heavier the ball, the lower the number and the smaller the bounce. If the ball is not caught, it will fall past the bottom of the screen out of the play area and register a game over.
For light and medium balls I am using a number that's high enough for the ball to clear the play area effectively (i.e the sine wave is large enough for the ball to fall out of bounds). But the problem I'm having is with the heavier balls, the ones with the smaller modifier to bendMod, the ball doesn't fall past the screen, because it's traveling in a sine wave, the ball gets to a minimum point and then comes back up.
I tried changing the value of bendMod in real time but that results in the ball traveling faster and it jerking around. I'm just not sure I'm going to be able to accomplish what I want using a Mathf.Sin() but I'm not sure what other options I have. Is there anything I can do to Mathf.Sin for this to work correctly? Or do I have to do something completely different than what I have here?