Ok, so after trying to figure it out by myself for the last three days I’m finally ready for some help :P.
I’m building circle pong like game and i’ve been trying to implement a mechanic where time slows down to almost a halt (0.0001) for everything ingame except the player. During this slowmotion mode i want to show the trajectory that the ball will take after time is back to normal, like this:
But when i’m using this code to calculate the trajectory:
LayerMask playerMask = LayerMask.GetMask("Player");
LayerMask enemyMask = LayerMask.GetMask("Enemy");
while (active){
Vector2 dir = this.transform.position - ball.transform.position;
RaycastHit2D hit = Physics2D.CircleCast(ball.transform.position, ball.GetComponent<CircleCollider2D>().radius, dir, Mathf.Infinity, playerMask | enemyMask);
if (hit.point != null) {
lr.SetPosition(0, ball.transform.position);
lr.SetPosition(1, hit.point);
Vector2 ballpos2d = new Vector2(ball.transform.position.x, ball.transform.position.y);
Vector2 ballTrajectory = Vector2.Reflect( hit.point - ballpos2d, hit.normal);
lr.SetPosition(2, ballTrajectory * 50);
}
yield return null;
}
}
The ball is set to dynamic rigidBody2D with no mass or drag and physics material2D with 0.3 bounce and no friction.The problem is that the ball off by some degree, like this:
So is there any way to calculate how physics based object would bounce off a collider?