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

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #19085
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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
    #19086
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Also as seen in the video, it falls really slow. Should I increase the gravity scale?

    #19087
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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.

    1. 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 MovePosition or AddForce) 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.
    2. 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!

    #19091
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

    #19092
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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);
        }
    }
    #19093
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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 Enemy and 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 Walker script, you’re using transform.position to 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 gravityScale to 12f in the Walker script, which should help with the fall speed. If you still find it falling too slowly, you can try increasing the gravityScale even 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.

    #19094
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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.

    #19095
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It sounds like you might be experiencing an issue where the MoveTowards function 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

    1. Separate Horizontal and Vertical Movement: Instead of using MoveTowards for 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));
        }
    }
    1. 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 MoveTowards function. The Rigidbody will apply gravity naturally.

    2. Check Rigidbody Settings:

      • Make sure that the Rigidbody2D settings have the Gravity Scale set appropriately (like the 12f you mentioned).
      • Ensure that the Mass of the Rigidbody2D is set correctly, as a lower mass can result in faster falling if gravity is applied.
    3. 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!

    #19096
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @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.

    #19100
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    1
    ::

    Oh Ok. Thank you for clarifying and for your help.

    has upvoted this post.
    #19101
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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!

    #19104
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Glad to be of help @nirvikrajbhandari

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: