Forum begins after the advertisement:
[Part 3] Enemy Clips Thru Wall
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 3] Enemy Clips Thru Wall
- This topic has 7 replies, 3 voices, and was last updated 2 days, 12 hours ago by
Terence.
-
AuthorPosts
-
December 3, 2025 at 8:17 am #19085::
My enemy can clip through walls. Here are two images and a video of the inspector for the wall, the inspector for the enemy, and the enemy clipping through the wall.
View post on imgur.com
December 3, 2025 at 8:20 am #19086December 3, 2025 at 8:22 am #19087::It sounds like you’re dealing with two different issues: the enemy clipping through walls and the slow fall speed. Let’s address them one at a time.
-
Enemy Clipping Through Walls:
- Check the colliders on both the enemy and the wall. Ensure that the wall collider is properly set up and that it’s a trigger if it needs to be (or not, depending on your design).
- Make sure the Rigidbody settings on the enemy are configured correctly. If the Rigidbody is set to Kinematic, it won’t be affected by physics forces, which might cause it to bypass colliders. If you’re using a Rigidbody, ensure that it’s not set to “Is Kinematic.”
- If you’re using a custom movement script, make sure that you’re using Rigidbody’s methods (like
MovePositionorAddForce) rather than directly modifying the transform, as this can interfere with the physics engine. - Consider increasing the thickness of your wall collider to prevent clipping.
-
Slow Falling:
- Yes, increasing the gravity scale is a common way to make objects fall faster in Unity. You can adjust this in the Rigidbody component of your enemy. A higher gravity scale will make the enemy fall faster.
- Additionally, check if there are any forces acting against gravity in your movement script or any other components that might affect the enemy’s fall speed, such as drag or buoyancy.
Make these adjustments and see if it improves the situation. If you’re still having issues, feel free to share your movement code or any specific settings you’re using for the colliders and Rigidbody components!
December 4, 2025 at 12:08 am #19091::If Alp’s response wasn’t very helpful, share all your enemy scripts as well. This will allow him to look through and pinpoint your issues better.
I don’t think your issue is with gravity scale. There is probably something in your script restricting the enemy’s physics, which is why the descend speed is slowed.
December 4, 2025 at 10:42 am #19092::Oh ok. Here is the enemy script.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { [SerializeField] protected float Enemy_Health; [SerializeField] protected float Recoil_Length; protected float Recoil_Timer; [SerializeField] protected float Recoil_Factor; [SerializeField] protected bool Is_Recoiling = false; protected Rigidbody2D Rigidbody; [SerializeField] protected Player_Controller Player; [SerializeField] protected Collider2D Player_Collider; [SerializeField] protected float Speed; [SerializeField] protected float Enemy_Damage; // Start is called before the first frame update protected virtual void Start() { Physics2D.IgnoreCollision(GetComponent<Collider2D>(), Player_Collider); } protected virtual void Awake() { Rigidbody = GetComponent<Rigidbody2D>(); Player = Player_Controller.Instance; } // Update is called once per frame protected virtual void Update() { if (Enemy_Health <= 0) { Destroy(gameObject); } if (Is_Recoiling) { if (Recoil_Timer < Recoil_Length) { Recoil_Timer += Time.deltaTime; } else { Is_Recoiling = false; Recoil_Timer = 0; } } } public virtual void Enemy_Hit(float Damage_Done, Vector2 Hit_Direction, float Hit_Force) { Enemy_Health -= Damage_Done; if (!Is_Recoiling) { Rigidbody.AddForce(-Hit_Force * Recoil_Factor * Hit_Direction); Is_Recoiling = true; } } protected void OnTriggerStay2D(Collider2D Other) { if(Other.CompareTag("Player") & !Player_Controller.Instance.Player_State.Invincible) { Debug.Log("Invincible" + Player_Controller.Instance.Player_State.Invincible); Debug.Log("Dashing" + Player_Controller.Instance.Player_State.Dashing); Attack(); Player_Controller.Instance.Hit_Stop_Time(0.1f, 5, 0.1f); } } protected virtual void Attack() { Player_Controller.Instance.Take_Damage(Enemy_Damage); } }and this is the walker script
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Walker : Enemy { // Start is called before the first frame update void Start() { base.Start(); Rigidbody.gravityScale = 12f; } protected override void Awake() { base.Awake(); } // Update is called once per frame protected override void Update() { base.Update(); if(!Is_Recoiling) { transform.position = Vector2.MoveTowards(transform.position, new Vector2(Player_Controller.Instance.transform.position.x, transform.position.y), Speed * Time.deltaTime); } } public override void Enemy_Hit(float Damage_Done, Vector2 Hit_Direction, float Hit_Force) { base.Enemy_Hit(Damage_Done, Hit_Direction, Hit_Force); } }December 4, 2025 at 10:47 am #19093::Thanks for sharing your enemy and walker scripts! I can see a few things you might want to check and modify to resolve the issues with clipping through walls and the slow fall speed.
1. Clipping Through Walls
Collider Setup
- Ensure that both the
Enemyand the walls have proper colliders attached. Make sure the wall colliders are not marked as triggers unless that’s the intended behavior. - Check that the enemy’s Rigidbody2D is not set to “Is Kinematic.” It should be set to Dynamic to be affected by physics.
Movement Method
- In your
Walkerscript, you’re usingtransform.positionto move the enemy. This can cause it to bypass colliders. Instead, use the Rigidbody’s physics methods to move the enemy:
protected override void Update() { base.Update(); if (!Is_Recoiling) { Vector2 targetPosition = new Vector2(Player_Controller.Instance.transform.position.x, transform.position.y); Vector2 newPosition = Vector2.MoveTowards(transform.position, targetPosition, Speed * Time.deltaTime); Rigidbody.MovePosition(newPosition); } }2. Slow Falling
You’ve already increased the
gravityScaleto12fin theWalkerscript, which should help with the fall speed. If you still find it falling too slowly, you can try increasing thegravityScaleeven more.3. Additional Checks
- Make sure there are no conflicting scripts or components that might be affecting the enemy’s movement or physics.
- Be cautious with any other scripts that might be modifying the Rigidbody properties simultaneously.
Summary
Switching from modifying the transform directly to using Rigidbody’s methods should help with the clipping issues. Adjusting the gravity scale further can improve the fall speed. Test these changes and see if they resolve the issues you’re encountering! If problems persist, feel free to share any additional code or context.
December 4, 2025 at 12:11 pm #19094::I believe the gravity problem is something to do with the movetowards function as when I am directly under it it falls faster than it should.
December 4, 2025 at 12:12 pm #19095::It sounds like you might be experiencing an issue where the
MoveTowardsfunction is affecting the perceived gravity of your enemy. When you’re directly underneath the enemy, the horizontal movement combined with the vertical position can create an illusion of faster falling due to the way you’re moving it.Suggestions to Fix the Gravity Issue
- Separate Horizontal and Vertical Movement:
Instead of using
MoveTowardsfor both horizontal and vertical positioning, you can handle vertical movement separately. This way, you can let the physics engine handle the falling:
protected override void Update() { base.Update(); if (!Is_Recoiling) { // Handle horizontal movement towards the player Vector2 targetPosition = new Vector2(Player_Controller.Instance.transform.position.x, transform.position.y); Vector2 newPosition = Vector2.MoveTowards(new Vector2(transform.position.x, 0), targetPosition, Speed * Time.deltaTime); // Update only the horizontal position Rigidbody.MovePosition(new Vector2(newPosition.x, transform.position.y)); } }-
Let Physics Handle Falling: Ensure you are not modifying the y-position of the enemy directly; let the Rigidbody handle the vertical gravity. You should not set the y-position in the
MoveTowardsfunction. The Rigidbody will apply gravity naturally. -
Check Rigidbody Settings:
- Make sure that the Rigidbody2D settings have the
Gravity Scaleset appropriately (like the 12f you mentioned). - Ensure that the
Massof the Rigidbody2D is set correctly, as a lower mass can result in faster falling if gravity is applied.
- Make sure that the Rigidbody2D settings have the
-
Debugging Movement: You can also add debug logs to check the actual velocity of the Rigidbody to see how fast it’s falling:
void FixedUpdate() { Debug.Log(Rigidbody.velocity); }By separating the movement calculations and letting the physics engine manage the vertical movement, you should see more consistent behavior in how the enemy falls compared to how it moves horizontally. Test these adjustments and see if it resolves the issue with the enemy’s falling speed!
December 4, 2025 at 3:38 pm #19096::@nirvikrajbhandari later in the series in Part 6, we will upgrade the Zombie script to a Crawler. That will contain more logic than the Zombie script, which will make the zombie grounded and have it move along the ground properly.
As long as you are able to hit the enemy, you can continue on with the tutorial.
December 4, 2025 at 10:01 pm #19100December 4, 2025 at 10:02 pm #19101::You’re welcome! I’m glad I could help clarify things for you. If you have any more questions or need further assistance with your code or game development, feel free to ask. Good luck with your project!
December 5, 2025 at 3:28 pm #19104 -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: