#Blocking the player from hitting the ball in certain directions 2D

7 messages · Page 1 of 1 (latest)

wind stirrup
#

why do you wait 1.5 seconds until disabling the collider again?

you could maybe try something like this
like in Update or FixedUpdate

Vector3 velocity = (transform.position - previousPosition) / Time.deltaTime;
        canHitUpwards = velocity.y >= 0;
        previousPosition = transform.position;
playerCollider.enabled = canHitUpwards; 
// Or do this instead
Physics2D.IgnoreCollision(playerCollider, ballCollider, !canHitUpwards);

https://docs.unity3d.com/ScriptReference/Physics2D.IgnoreCollision.html
when taking the velocity that way you'd have to be careful though if you also change your Time.timeScale, since deltaTime could get zero
you could also make it so canHitUpwards is only true when the velocity of the player rag exceeds the velocity of the ball upwards as well, or optionally also include a minimal velocity required for the hit to be valid (although I wouldn't do the last part)
canHitUpwards = velocity.y >= 0 && velocity.y >= ballVelocity.y; (optionally you could do an || here, because sometimes the player rag could move downwards, but if the ball moves downwards faster, you could still hit the ball when the ball is being above even if the player rag moves downwards).
Also, another check you could do is simple that if the ball is above the rag + some tolerance distance, you know you always hit it from below

#

Sometimes, the game ignores collisions even when hitting upwards, especially at higher speeds.
that's generally something that happens, you can set the player rag or the ball collision type on the rigidbody to continuous (or set it to continuous on both) but then it can still happen sometimes. the solution could be to do a manual CircleCast from the position of the ball in the prev frame to the current frame position (or optionally predictive, use the current ball position and cast it
https://docs.unity3d.com/ScriptReference/Physics2D.CircleCast.html

then you can manually react if you have a hit, like newBallPosition = hit.point + hit.normal * ballRadius; or also set the velocity with reflect newBallVelocity = Vector2.Reflect(ballVelocity, hit.normal);

#

The paddle should not be dragged down by the ball.
seems like the paddle setup has a dynamic rigidbody, not a kinematic one

#

although then you don't really have an applied velocity on the player I guess,
hmhh tricky, you could try to handle everything manually too, or give the paddle an insane weight perhaps,
but the later is just a hack tbh

#

important section

It is important to understand that the actual position change will only occur during the next physics update therefore calling this method repeatedly without waiting for the next physics update will result in the last call being used. For this reason, it is recommended that it is called during the FixedUpdate callback.

loud atlas