Forum begins after the advertisement:


Crawler can’t flip

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #16300
    Pool ForReal
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    my crawler can’t be flip and the crawler can’t die i have attack many times

    this my enemy code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Centipede : Enemy
    {
        [SerializeField] private float flipWaitTime;
        [SerializeField] private float ledgeCheckX;
        [SerializeField] private float ledgeCheckY;
        [SerializeField] private LayerMask whatIsGround;
        float timer;
    
        // Start is called before the first frame update
        protected override void Start()
        {
            base.Start();
            rb.gravityScale = 12f;
        }
    
        protected override void Awake()
        {
            base.Awake();
        }
    
        // Update is called once per frame
        
    
        protected override void UpdateEnemyStates()
        {
            switch (currentEnemyState)
            {
                case EnemyStates.Centipede_idle:
                    Vector3 _ledgeCheckStart = transform.localScale.x > 0 ? new Vector3(ledgeCheckX, 0) : new Vector3(ledgeCheckX, 0);
                    Vector2 _wallCheckDir = transform.localScale.x > 0 ? transform.right : -transform.right;
    
                    if (!Physics2D.Raycast(transform.position + _ledgeCheckStart, Vector2.down, ledgeCheckY, whatIsGround)
                        || Physics2D.Raycast(transform.position, _wallCheckDir, ledgeCheckX, whatIsGround))
                    {
                        ChangeState(EnemyStates.Centipede_Flip);
                    }
    
                    if (transform.localScale.x > 0)
                    {
                        rb.velocity = new Vector2(speed, rb.velocity.y);
                    }
                    else
                    {
                        rb.velocity = new Vector2 (-speed, rb.velocity.y);
                    }
                    break;
    
                case EnemyStates.Centipede_Flip:
                    timer = Time.deltaTime;
    
                    if (timer > flipWaitTime)
                    {
                        timer = 0;
                        transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
                        ChangeState(EnemyStates.Centipede_idle);
                    }
                    break;
            }
        }
    }
    
    #16301
    Chloe Lim
    Level 9
    Moderator
    Helpful?
    Up
    1
    ::

    Hi, in your flip case you need to add the time delta time to your timer
    timer += Time.deltaTime;

    For your enemy hit, send your main enemy script so that I could check what could be wrong there

    has upvoted this post.
    #16309
    Pool ForReal
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    this my enemy code

    using System.Collections;
    using System.Collections.Generic;
    using Unity.VisualScripting;
    using UnityEngine;
    
    public class Enemy : MonoBehaviour
    {
        [SerializeField] protected float health;
        [SerializeField] protected float recoilLength;
        [SerializeField] protected float recoilFactor;
        [SerializeField] protected bool isRecoiling = false;
    
        [SerializeField] protected PlayerController player;
        [SerializeField] protected float speed;
    
        [SerializeField] protected float damage;
    
        protected float recoilTimer;
        protected Rigidbody2D rb;
        protected SpriteRenderer sr;
    
        protected enum EnemyStates
        {
            //Centipede
           Centipede_idle,
           Centipede_Flip,
            //Bat
            Bat_Idle,
            Bat_Chase,
            Bat_Stunned,
            Bat_Death,
        }
        protected EnemyStates currentEnemyState;
        // Start is called before the first frame update
        protected virtual void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            sr = GetComponent<SpriteRenderer>();
        }
    
        protected virtual void Awake()
        {
            rb = GetComponent<Rigidbody2D>();
            player = PlayerController.Instance;
        }
        // Update is called once per frame
        protected virtual void Update()
        {
            if(isRecoiling)
            {
                if(recoilTimer < recoilLength)
                {
                    recoilTimer += Time.deltaTime;
                }
                else
                {
                    isRecoiling = false;
                    recoilTimer = 0;
                }
            }
            else
            {
                UpdateEnemyStates();
            }
        }
    
        public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce)
        {
            health -= _damageDone;
            if(!isRecoiling)
            {
               rb.velocity = _hitForce * recoilFactor * _hitDirection;
            }
        }
    
        protected void OnTriggerStay2D(Collider2D _other)
        {
            if(_other.CompareTag("Player") && !PlayerController.Instance.pState.invincible)
            {
                Attack();
                PlayerController.Instance.HitStopTime(0, 5, 0.5f);
            }
        }
    
        protected virtual void UpdateEnemyStates() { }
    
        protected void ChangeState(EnemyStates _newState)
        {
            currentEnemyState = _newState;
        }
        protected virtual void Attack()
        {
            PlayerController.Instance.TakeDamage(damage);
        }
    }
    
    #16311
    Chloe Lim
    Level 9
    Moderator
    Helpful?
    Up
    0
    ::

    Ok, you need to add the if statement for death in your crawler script
    in updateenemystates in your crawler script, add this

            if(health <= 0)
            {
                Death(0.05f);
            }

    Then in your main enemy script add the function for death

        protected virtual void Death(float _destroyTime)
        {
            Destroy(gameObject, _destroyTime);
        }
    #16312
    Pool ForReal
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    but my crawler still not flip or i must add wall ?

    #16318
    Chloe Lim
    Level 9
    Moderator
    Helpful?
    Up
    0
    ::

    your crawler state idle for ledgecheck is missing the negative ledgexcheck
    Vector3 _ledgeCheckStart = transform.localScale.x > 0 ? new Vector3(ledgeCheckX, 0) : new Vector3(-ledgeCheckX, 0);
    also ensure the platforms in your game have the ground tag

    #16337
    Pool ForReal
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    thank you its working

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

Go to Login Page →


Advertisement below: