This is my first time making a game, I’ve scrapped together a patchwork of code, and I’ve taken my notes. When I tried to put the code together though, everything seemed to be working. The animation transitions were good, my character’s controls responded well to jumping and horizontal movement. But when it came to the health and damage system, when the character walks up to the enemy and hits the supposed hitbox, it takes a few extra hits-- more than necessary-- to get the enemy//gameObject to actually disappear. Are there any fixes to this?
_
Here’s my script:
using System.Collections;
using System.Collections.Generic
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
private float moveInput;
private Rigidbody2D rb;
private Animator anim;
private string midjump;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
private bool facingRight = true;
private float timeBtwAttack;
public float startTimeBtwAttack;
public Transform attackPos;
public LayerMask whatisEnemies;
public float attackRange;
public int damage;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if ((Input.GetKeyDown(KeyCode.W)) && midjump == "n")
{
rb.velocity = new Vector3(0, 7, 0);
midjump = "y";
}
if (rb.velocity.y == 0)
midjump = "n";
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetKeyDown(KeyCode.W))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
} //jump
if (Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("isAttacking", true);
}
else
{
anim.SetBool("isAttacking", false);
} //attack
if (timeBtwAttack <= 0)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Collider2D[]enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatisEnemies);
for (int i = 0; i < enemiesToDamage.Length; i++) //this is a loop
{
enemiesToDamage*.GetComponent<Enemy>().TakeDamage(damage);*
}
}//Damageee
timeBtwAttack = startTimeBtwAttack;
}
else
{
timeBtwAttack -= Time.deltaTime;
}//Attack delay
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
} //attackRange vis
private void FixedUpdate() // This is horizontal movement
{
float moveInput = Input.GetAxisRaw(“Horizontal”);
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (moveInput == 0)
{
anim.SetBool(“isRunning”, false);
}
else
{
anim.SetBool(“isRunning”, true);
}
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
//Vhat is flip
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
And here’s the script for the enemy;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int health;
public GameObject bloodEffect;
public void TakeDamage(int damage)
{
health -= damage;
Instantiate(bloodEffect, transform.position, Quaternion.identity);
}
void Update()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
}