Forum begins after the advertisement:
Crawler can’t flip
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › Crawler can’t flip
- This topic has 6 replies, 2 voices, and was last updated 1 week ago by Pool ForReal.
Viewing 7 posts - 1 through 7 (of 7 total)
-
AuthorPosts
-
November 12, 2024 at 5:29 am #16300::
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; } } }
November 12, 2024 at 11:14 am #16301November 12, 2024 at 8:20 pm #16309::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); } }
November 13, 2024 at 12:29 am #16311::Ok, you need to add the if statement for death in your crawler script
in updateenemystates in your crawler script, add thisif(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); }
November 13, 2024 at 2:09 am #16312November 13, 2024 at 2:43 pm #16318::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 tagNovember 15, 2024 at 4:56 am #16337 -
AuthorPosts
Viewing 7 posts - 1 through 7 (of 7 total)
- You must be logged in to reply to this topic.
Advertisement below: