Forum begins after the advertisement:
[Part 3.5] After jumping and falling, damage is not counted.
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 3.5] After jumping and falling, damage is not counted.
- This topic has 28 replies, 4 voices, and was last updated 6 months, 2 weeks ago by
Da.
-
AuthorPosts
-
February 7, 2025 at 3:12 am #17189::
Hi. Faced with such a problem. Hits are registered, but not everywhere. For example, when gg falls, they don’t want to register hits. It works, but it’s weird. You have to increase the hitbox itself, but this is poorly reflected when the gg does not fall (the hitbox becomes much larger than it should be). I did everything exactly as in the video. https://www.youtube.com/watch?v=HkuihIuKAm0&list=PLgXA5L5ma2BvEqzzeLnb7Q_4z8bz_cKmO&index=9
February 7, 2025 at 5:11 am #17190::It sounds like you’re dealing with an issue related to hit detection in a game, particularly when your game object (gg) falls. If hits are not being registered consistently, there are a few things you could check or try to improve the situation:
-
Hitbox Size and Position: Ensure that the hitbox is appropriately sized and positioned. If the hitbox is too small, it might not register hits when the object falls. However, increasing the hitbox too much can lead to unintended collisions when the object is not falling. You might consider using a dynamic hitbox that adjusts its size based on the object’s state (falling vs. not falling).
-
Collision Layer: Verify that the layers involved in collision detection are set correctly. Sometimes, objects may not register hits if they are on different collision layers or if the collision matrix does not allow for interactions between the layers.
-
Physics Update Rate: Check the physics update rate of your game. If the game’s physics are updated at a low frequency, it might lead to missed collisions. Increasing the frequency of physics updates can help improve hit detection.
-
Raycasting: Instead of relying solely on hitboxes, consider using raycasting for more precise hit detection. Raycasting can provide more accurate collision detection, especially for falling objects.
-
Debugging: Use debugging tools to visualize the hitbox and see where it is in relation to other objects. This can help you understand why hits are not being registered in certain situations.
-
Events and Triggers: Ensure that you have the correct events set up for registering hits. For example, check if you are using OnCollisionEnter, OnTriggerEnter, or similar methods correctly.
-
Game Engine Settings: Depending on the game engine you are using, there may be settings that affect how collisions are detected. Review the documentation for your specific engine to ensure everything is configured properly.
-
Check the Video Again: Since you mentioned following a video tutorial, double-check the steps in the video to ensure that you have not missed any important details or settings.
If you provide more specific details about the game engine or the code you are using, I can offer more targeted advice.
February 7, 2025 at 3:30 pm #17191::I use Unity. The layers are set correctly, I don’t think the problem is with them, although I’m not sure (Everything works fine on the ground and during the jump itself, but not in the fall – the function itself works, I checked with a debugger). The code for registering hits.
[Header("Attack Settings")] [SerializeField] private float TimeBeetwenAttack; bool attack = false; private float TimeSinceAttack; [SerializeField] Transform SideAttackTransform; [SerializeField] Vector2 SideAttackArea; [SerializeField] LayerMask attackableLayer; [SerializeField] private float damage; void Update() { if (isDashing) { return; } TimeSinceAttack += Time.deltaTime; GetInputs(); Attack(); Flip(); Move(); Jump(); StartDash(); anim.SetBool("NotDash", isDashing); } private void Attack() { if (Input.GetButtonDown("Attack") && TimeSinceAttack >= TimeBeetwenAttack) { TimeSinceAttack = 0f; anim.SetTrigger("Attacking"); Hit(SideAttackTransform, SideAttackArea); } } void Hit(Transform _attackTransform, Vector2 _attackArea) { Collider2D[] objectsToHit = Physics2D.OverlapBoxAll(_attackTransform.position, _attackArea, 0, attackableLayer); if (objectsToHit.Length > 0) { Debug.Log("Hit!"); } else if (objectsToHit.Length <= 0) { Debug.Log("No Hit!"); } } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireCube(SideAttackTransform.position, SideAttackArea); }
February 7, 2025 at 3:52 pm #17192::@Da can you use ScreenToGif to record a clip of the issue? https://www.screentogif.com/
Let me know if you see any errors on your Console window as well.
February 7, 2025 at 5:11 pm #17194::It looks like you’re trying to handle an attack mechanism in Unity, but you’re facing issues with detecting hits while falling. Since you’ve mentioned that everything works when the character is on the ground or during a jump, this indicates that the logic for the attack itself may be functioning correctly, but there might be some issues related to the attack detection when falling.
Here are a few suggestions to help you troubleshoot and potentially resolve the issue:
-
Check the Attack Area Size:
- Ensure that the
SideAttackArea
variable is set correctly in the inspector. If it’s too small or positioned incorrectly, it may not overlap with any colliders while falling.
- Ensure that the
-
Adjust the Position of the Attack Transform:
- When you’re falling, the position of
SideAttackTransform
could change. Make sure that it is correctly positioned relative to your character’s collider and that it is where you expect it to be when the attack is triggered.
- When you’re falling, the position of
-
Debugging the Attack Position:
- Add a debug log inside the
Hit
method to print the position of_attackTransform
and the dimensions of_attackArea
. This will help you visualize whether the hit detection is being executed in the correct location.
Debug.Log($"Attack Position: {_attackTransform.position}, Area: {_attackArea}");
- Add a debug log inside the
-
Layer Masks:
- Double-check that the
attackableLayer
is set correctly in the inspector. Ensure that the objects you want to hit are indeed on that layer.
- Double-check that the
-
Physics Settings:
- Sometimes, the physics settings may impact the detection. Make sure that the
Rigidbody2D
component on your character has the correct settings (e.g., Gravity Scale, Collision Detection).
- Sometimes, the physics settings may impact the detection. Make sure that the
-
Alternative Hit Detection Method:
- If you’re still having issues, consider using
Physics2D.OverlapCircle
for hit detection instead ofOverlapBox
, especially if your attack has a more circular motion. This could give you more reliable hit detection depending on your character’s attack animation.
Collider2D[] objectsToHit = Physics2D.OverlapCircleAll(_attackTransform.position, _attackArea.x / 2, attackableLayer);
- If you’re still having issues, consider using
-
Animation Events:
- If your attack animation has a specific frame where you want the hit detection to happen, consider using animation events to call the
Hit
method at the right time in the animation.
- If your attack animation has a specific frame where you want the hit detection to happen, consider using animation events to call the
-
Collision Layers:
- Ensure that the collision layers are set up properly in the Unity Physics settings (Edit > Project Settings > Physics2D). Make sure the layers are configured to interact as you expect.
By following these suggestions, you should be able to identify the root cause of the issue and correct it. If you’re still having trouble after trying these methods, feel free to share more details about your setup or any errors you’re encountering.
February 7, 2025 at 5:33 pm #17195::Yes, of course. Thank you for responding so promptly) It is divided into 3 parts, since the imgur is limited to 60 seconds.
View post on imgur.com
View post on imgur.com
View post on imgur.com
The video shows that the layers are installed correctly. The hitbox area is also visible. The recording of hits in different situations is shown. The problem is that hits are recorded very strangely when the character falls (the video shows that the hitbox hits the enemy’s layer, but hits are not recorded). What’s even stranger is that registration is completely normal during the jump and movement. The whole Internet has been crawling, but it’s like I’m the only one with this problem)
February 7, 2025 at 5:35 pm #17196February 7, 2025 at 7:11 pm #17197::It seems like you’re encountering an issue with hit detection in your game when your character is falling. This can be a tricky problem to troubleshoot, but here are some common areas to investigate:
-
Hitbox Configuration: Ensure that the hitbox for your character is correctly configured for the falling state. Sometimes, the hitbox might change or not be active when the character is falling.
-
Physics Settings: If you’re using a physics engine, verify that the collision settings are consistent during falling. Check if the collision layers and masks are set up correctly, and ensure that both the character and the enemy are on the correct layers to interact.
-
State Management: Look at how you’re managing different states (falling vs jumping). It’s possible that the hit detection logic is being bypassed or altered when your character is in the falling state.
-
Debugging Hit Detection: Add debug logs or visual indicators that show when a hit is detected. This can help you see if the hit detection logic is being triggered but not registering hits as expected.
-
Gravity and Movement: Consider how gravity and movement are affecting the hitbox. If the character is moving too quickly while falling, it might be passing through the enemy without registering a hit. You might need to implement a raycast or continuous collision detection for faster falling objects.
-
Layer and Collision Settings: Double-check your layer-based collision settings. Ensure that your character’s hitbox and the enemy’s hitbox are configured to collide with each other in the physics settings.
-
Frame Rate Considerations: If your game has a variable frame rate, ensure that your hit detection logic accounts for frames where the character may fall through the enemy due to rapid movement.
If you could provide more details about the game engine or framework you’re using and the code related to hit detection, I’d be happy to help you troubleshoot further!
February 7, 2025 at 7:34 pm #17198::Hi! for testing purposes i would create another one of those attackable walls but much much taller and spawn my player gameobject at a higher position, so if i start the game the player object falls down along the wall for a few seconds which would give you a time frame to attack multiple times and see if hit detection is still inconsistant. maybe lower gravity settings for longer fall time frame. It for sure does not solve the problem, but it might help isolating whats causing the inconsistancy
and 1 other person have upvoted this post. February 7, 2025 at 7:44 pm #17199::Yes, it’s as you say. I set the gravity low and made the character fall. With it, the registration of hits works perfectly. But now the next question has arisen, why is this happening and how to fix it? And why then does the hit registration work when the character jumps. These points are not entirely clear to me, I would appreciate it if you would answer them.
February 7, 2025 at 7:48 pm #17200February 7, 2025 at 7:50 pm #17201::Although, this would solve the problem of failure of the method itself. But this seems to be a different kind of problem.
February 7, 2025 at 7:54 pm #17202::When i was watching your clips i could spot some frames where the Physics2D.OverlapBox was drawn outside of the attackable walls. i mean those few frames where the player object goes from jumping into falling state
View post on imgur.com
i think, since it is so difficult to time with those small walls, you sometimes attacked while the OverlapBox was not overlapping with the attackable wall, which would mean no collider to detect
and 1 other person have upvoted this post. February 7, 2025 at 8:03 pm #17203::i tried to upload a image but i failed, i am new to forums >.< “the small red line = overlapbox” was supposed to be the description of the image xD
- 1 anonymous person
February 7, 2025 at 8:08 pm #17204::Me too) Do you know how to solve this problem? (I understood about the intersection of the line and the collider itself, but usually it was clear that the attack should have passed, but it did not happen). There may be some other method. Or you need to make a collider. There is an idea, you can make the method work for a few seconds, and not just at the moment of pressing the button.
February 7, 2025 at 8:11 pm #17205::It sounds like you’re dealing with an issue related to hit registration in a game, possibly when using a physics system for character movement and attacks. Here are some thoughts to help you troubleshoot and potentially resolve the problems you’re facing:
Understanding Hit Registration
-
Gravity and Collision Detection:
- When you set gravity low and the character falls slowly, the physics engine might have more time to detect collisions, which can improve hit registration. If your character is moving too quickly, the physics engine might skip over collisions due to how frames are processed.
-
Jumping vs. Falling:
- When the character jumps, the physics behavior changes. Depending on how you’ve set up your collision detection, it may be more effective at detecting hits when the character is airborne. This could be due to the angle of attack or the speed of the character during the jump.
-
Attack Method Frequency:
- If the “Attack” method is not being called frequently enough, you might consider placing the attack logic in a way that it checks for hits more continuously (e.g., using a timer, or checking for inputs in the
Update
method).
- If the “Attack” method is not being called frequently enough, you might consider placing the attack logic in a way that it checks for hits more continuously (e.g., using a timer, or checking for inputs in the
Possible Solutions
-
Collider Setup:
- Ensure that your colliders are appropriately sized and positioned. If the collider for the attack is too small or incorrectly placed, it might not register hits as expected.
-
Raycasting:
- If you haven’t already, consider using raycasting to check for hits. This can provide more accurate collision detection for attacks, especially if you’re targeting a specific direction or area.
-
Hitbox System:
- Implement a hitbox system where you have a separate collider specifically for the attack. This hitbox can be activated when the attack is initiated and checked for collisions against enemies.
-
Physics Layers:
- Check your physics layers and collision settings. Ensure that your character’s attack collider is set to interact with the enemy colliders.
-
Debugging:
- Use debug lines or gizmos to visualize the colliders and raycasts in your game scene. This can help you see whether the attack collider is actually intersecting with the target during the attack.
-
Reviewing Timing:
- Ensure that the timing of the attack animation and the collision detection logic are in sync. If the attack happens too quickly or too slowly relative to the physics updates, it might miss hits.
Summary
To fix your hit registration issue, you may want to explore the collider setup, attack method frequency, and consider using raycasting or a hitbox system. Debugging tools can greatly aid in visualizing what’s happening in your game, allowing you to adjust accordingly. If you provide more specifics about your implementation (e.g., the game engine you’re using, the code for your attack method), I could offer more tailored advice!
February 7, 2025 at 8:27 pm #17206::Does the error still occur on the small walls (when jumping/falling) after increasing the size of the SideAttackArea vector by, idk 4 times? so that the red box becomes much taller If it doesnt occur, you probably got deceived by the length of the animation of your attack. the overlapbox only gets drawn during the first frame of your attack, all the other frames of your attack would not trigger a hit on the wall (i am not 100% sure about that XD, but makes sense to me). But if you want all frames to trigger a hit, there is for sure some way to do that
February 7, 2025 at 8:45 pm #17207::When the SideAttackArea is increased, the problem disappears. Apparently, the problem is really a slight discrepancy between the animation and the impact itself. I don’t know if it’s a crutch, but I solved the problem by introducing a small delay when registering a hit (a character’s hit in my game should be instantaneous, so I think that will be enough). Although it is a very interesting question how to make the attack itself last the entire animation.
February 7, 2025 at 8:46 pm #17208::Thanks for the help. To be honest, I didn’t expect to get an answer to my question so quickly. I’m still new to unity, but I want to make games)
February 7, 2025 at 9:11 pm #17209::I am glad i was able to help :) But i am afraid i can not help you with the question “how to make all animation frames trigger an hit”, since i am new to unity as well. But that might be a topic for a new post. Currently making my first game with these tutorials as well :)
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: