Ball is colliding further and further forward despite the math showing that it shouldn't?
I know the title is confusing but so is the question haha. Anyway, the scenario is this:
A ball is moving forward (positive Z axis) at 12 units/second
Every 24 units is a platform, which the ball lands on and is given an upward velocity of 10 units/second (the gravity is -10 units/second, therefore the ball is in the air for 2 seconds and will travel the whole 24 units between platforms)
Every 10 platforms (determined by a score that is increased every time a platform is hit), the speed of the ball increases by 3 units/second (12, 15, 18, etc.)
Additionally, the upward velocity the ball is given when it hits a platform is actually calculated by 120 / current speed (mathematically this will keep the ball moving 24 units between each platform)
The platforms are all the same height and should be collided with at the same interval
The platforms have a kinematic rigidbody (which is turned non kinematic when it is collided with so that it can fall)
Both the ball and the platforms have no friction on their colliders
Math:
Code (there's also an update function and a few more variables in this script but they don't pertain to the question):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallScript : MonoBehaviour
{
public Rigidbody rb;
public GameObject scoreComponent;
public float currentSpeed = 12;
public int newScore = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.velocity = new Vector3 (0, 0, currentSpeed);
currentSpeed = 12;
}
void OnCollisionEnter(Collision collision)
{
if ((collision.gameObject.name == "Platform" || collision.gameObject.name == "FalsePlatform") && transform.position.y >= 0.58)
{
newScore += 1;
currentSpeed = (Mathf.Floor(newScore / 10) * 3) + 12;
rb.velocity = new Vector3 (0, 120 / currentSpeed, currentSpeed);
print(newScore + " " + transform.position);
collision.gameObject.GetComponent<Rigidbody>().isKinematic = false;
collision.gameObject.GetComponent<Rigidbody>().velocity = new Vector3 (0, -10, 0);
scoreComponent.GetComponent<ScoreDisplayScript>().score = newScore;
}
}
}
The problem is that, eventually, the ball begins moving a little bit faster than the platforms and on each successive platform is relatively farther and farther forward until it falls off the front. Specifically, the Z of the position vector of the ball printed at the end increases by the correct amount each time until the score is 20 (starts at 11.3, then 35.3, 59.3, 83.3... until the score of 20 prints 467.3 and then 21 prints 491.4, then 515.5, 539.6, 563.7...) I'm no expert at Unity but I have absolutely no idea what could be causing this. My guess is it has to do with a possible delay on the onCollisionEnter function or the inaccuracy of the float values (although I would assume the number would have to be a lot larger for that to be an issue). I understand that this is probably a stumper and I really appreciate any input that can be given.