Forum begins after the advertisement:
[part 6] crawler enemy bugging
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [part 6] crawler enemy bugging
- This topic has 3 replies, 3 voices, and was last updated 8 months, 2 weeks ago by
Aka.
-
AuthorPosts
-
December 9, 2023 at 3:25 am #12549
Niyam Shah
Participant::my crawler enemies movement distance is depending on the speed i set
50 speed will be fast with a long distance while 10 speed will be slow with a rlly short distance
also plssssss allow the blog part 6-10 to be free bc they r so vital to check if you have code errors bc the videos r sooo fast
using UnityEngine; public class Crawler : 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; } // Update is called once per frame protected override void UpdateEnemyStates() { switch (currentEnemyStates) { case EnemyStates.Crawler_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.Crawler_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.Crawler_Flip: timer += Time.deltaTime; if (timer > flipWaitTime) { timer = 0; transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y); ChangeState(EnemyStates.Crawler_Idle); } break; } } }
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 float speed; [SerializeField] protected float damage; protected float recoilTimer; protected Rigidbody2D rb; protected enum EnemyStates { //crawler Crawler_Idle, Crawler_Flip } protected EnemyStates currentEnemyStates; // Start is called before the first frame update protected virtual void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame //enemy death and recoil protected virtual void Update() { if (isRecoiling) { if (recoilTimer < recoilLength) { recoilTimer += Time.deltaTime; } else { isRecoiling = false; recoilTimer = 0; } } else { UpdateEnemyStates(); } } //enemy takes damage and has recoil public virtual void EnemyHit(float _damageDone, Vector2 _hitDirection, float _hitForce) { health -= _damageDone; if (!isRecoiling) { rb.AddForce(-_hitForce * recoilFactor * _hitDirection); } } //make player take damage when trigger collider touches player collider - not when player is invincible protected void OnCollisionStay2D(Collision2D _other) { if (_other.gameObject.CompareTag("Player") && !PlayerController.Instance.pState.invincible) { Attack(); PlayerController.Instance.HitStopTime(0, 5, 0.5f); } } protected virtual void UpdateEnemyStates() { } protected void ChangeState(EnemyStates _newState) { currentEnemyStates = _newState; } protected virtual void Attack() { PlayerController.Instance.TakeDamage(damage); } }
December 9, 2023 at 8:23 pm #12550Niyam Shah
Participant::ok i found the fix
the ledge check y value was too low – not sure why but a really low value makes it constantly detect a ledge – i set mine to 1.5
the ledge check value should be higher – i set mine to 3
they work in opposites]
really strange but it works now
December 10, 2023 at 11:03 pm #12566December 22, 2023 at 5:02 pm #12728Aka
Participant::Always use Gizmos when you use Raycast. The ray check depend of your asset. If you dont use exactly the same asset, the value must be different. Gizmos help you
Here by example :
private void OnDrawGizmosSelected()
{
_ledgeCheckStart = transform.localScale.x > 0 ? new Vector3(ledgeCheckX, 0) : new Vector3(-ledgeCheckX, 0);
Gizmos.color = Color.blue;
Gizmos.DrawRay(transform.position + _ledgeCheckStart, Vector2.down * ledgeCheckY);
} -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: