I want to implement my 2d player doggy hitting, I have already frames available for hitting left side and right side. Here you have those, left and right hitting animation frames:
Now I want to hit the coming ball from either direction so what kind of implementation shall I require to implement for this?
Basically, the ball is coming from the left or right direction and we have to hit it otherwise it gets collide with player doggy and it will be game over.
Now the ball has circle collider attached with the 2d rigid body now at the right frame I require to detect that and hit the coming ball. Please provide me some suggestion for this.
I would probably create two extra colliders that are set on IsTrigger for detecting where the ball is coming from. One on the right of your doggy and the other one on the left. Then in a script you can use the OnTriggerEnter2D() method to select the correct animation. If I understand your issue correctly, this should work just fine, even though there are probably better/more efficient ways.
You understand my point correctly and you are in the right direction
I did exactly the same thing that you mentioned above already and attached box colliders on the left and right side ears.
Let me give you a more clear idea after this regarding my problem.
Based on a single touch on the screen, left or right hitting option get selected. So maybe the ball is coming from the left side and right hit option to get selected, maybe ball is coming from right side and right hit option to get selected and vice versa for other variations.
Now some times what is happening we are hitting and its animation are playing then also ball goes out of the collider and get a collision with the doggy body and its game over - its important for me to stop this because of its ruining actual game enjoyment.
Like this way, I have attached colliders:
I have already added Rigidbody component with doggy.
Please give me a little bit more suggestion into this.
I would do a somewhat custom system, which would go like this
// Control these from within your code, when you enable the colliders.
bool isLeftEarActive, isRightEarActive;
public Collider2D HitBox; // Set via Inspector
void OnCollisionEnter2D(Collision2D other) {
bool BallWasOnRight = other.point.x > HitBox.transform.position.x;
bool BallWasOnLeft = !BallWasOnRight;
if (BallWasOnLeft && !isLeftEarActive) {
Die();
}
else if (BallWasOnRight && !isRightEarActive) {
Die();
}
else {
RepelBall();
}
}
You can remove the RepelBall(); if you want Unity’s physics to take care of it.