Forum begins after the advertisement:
[Part 6] My charger enemy can’t walk
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 6] My charger enemy can’t walk
- This topic has 1 reply, 2 voices, and was last updated 1 year, 1 month ago by Terence.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
November 21, 2023 at 2:17 pm #12259::
Charger.cs
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; public class Charger : Enemy { [SerializeField] private float ledgeCheckX; [SerializeField] private float ledgeCheckY; [SerializeField] private float chargeSpeedMultiplier; [SerializeField] private float chargeDuration; [SerializeField] private float jumpForce; [SerializeField] private LayerMask whatIsGround; float timer; // Start is called before the first frame update protected override void Start() { base.Start(); ChangeState(EnemyStates.Charger_Idle); rb.gravityScale = 12f; } protected override void UpdateEnemyState() { if (health <= 0) { Death(0.05f); } Vector3 _ledgeCheckStart = transform.localScale.x > 0 ? new Vector3(ledgeCheckX, 0) : new Vector3(-ledgeCheckX, 0); Vector2 _wallCheckDir = transform.localScale.x > 0 ? transform.right : -transform.right; switch (GetCurrentEnemyState) { case EnemyStates.Charger_Idle: if (!Physics2D.Raycast(transform.position + _ledgeCheckStart, Vector2.down, ledgeCheckY, whatIsGround) || Physics2D.Raycast(transform.position, _wallCheckDir, ledgeCheckX, whatIsGround)) { transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y); } RaycastHit2D _hit = Physics2D.Raycast(transform.position + _ledgeCheckStart, _wallCheckDir, ledgeCheckX * 10); if (_hit.collider != null && _hit.collider.gameObject.CompareTag("Player")) { ChangeState(EnemyStates.Charger_Surprissed); } break; case EnemyStates.Charger_Surprissed: rb.velocity = new Vector2(0, jumpForce); ChangeState(EnemyStates.Charger_Charge); break; case EnemyStates.Charger_Charge: timer += Time.deltaTime; if(timer < chargeDuration) { if(Physics2D.Raycast(transform.position, Vector2.down, ledgeCheckY, whatIsGround)) { if (transform.localScale.x > 0) { rb.velocity = new Vector2(speed * chargeSpeedMultiplier, rb.velocity.y); } else { rb.velocity = new Vector2(-speed * chargeSpeedMultiplier, rb.velocity.y); } } else { rb.velocity = new Vector2(0, rb.velocity.y); } } else { timer = 0; ChangeState(EnemyStates.Charger_Idle); } break; } } protected override void ChangeCurrentAnimation() { if(GetCurrentEnemyState == EnemyStates.Charger_Idle) { anim.speed = 1; } if (GetCurrentEnemyState == EnemyStates.Charger_Charge) { anim.speed = chargeSpeedMultiplier; } } } </code>
My charger enemy cannot walk, but it can charge when the Player is in front of it
My enemy can't walk
View post on imgur.com
November 21, 2023 at 2:40 pm #12261::The issue is probably because your
currentEnemyState
property is not updating correctly.Do you know how to enter Debug Mode on the Inspector? You can select your enemy in Debug Mode on the Inspector, and it will expose all private variables on the component as well. Once done, you can look at the
currentEnemyState
property and see if it is updating correctly as your enemy is going about its business. -
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.
Advertisement below: