I tried creating a melee combat system with the help of Blackthornpods melee combat video. The hit detection works fine but i'm having issues with knock back. I found out how to make it work but the enemy doesn't always knockback and the system feels kind of clunky but I honestly don't know how to fix any of this. I feel like it might have something to do with how the enemy attacks the player which is a coded dash because of the way I wanted to do it, I don't think I could have done it with animation. I wanted the enemy to dash through the player which is what I achieved. Anyways I think it might have to do with how the enemy dashes and how the enemy moves towards the player but i'm not sure. Here is the code for the enemy script and the enemy attack script. I apologize if it's messy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public int health;
public float speed;
[HideInInspector]
public bool hit;
private bool knockedback;
public float knockback;
private float dazedTime;
public float startDazedTime;
public int damage;
public float stopDistance;
public int dropChance;
public GameObject[] pickups;
[HideInInspector]
public Transform playerPos;
public AudioSource hurt;
public AudioClip hurtClip;
public GameObject deathEffect;
private Rigidbody2D rb;
private Vector2 moveDirection;
private Animator anim;
EnemyMelee attack;
private void Start()
{
playerPos = GameObject.FindGameObjectWithTag("Player").transform;
rb = GetComponent();
anim = GetComponent();
attack = GetComponent();
hit = false;
hurt.clip = hurtClip;
}
private void Update()
{
if (playerPos == null)
{
anim.SetBool("isIdle", true);
}
if (health <= 0)
{
int randomNumber = Random.Range(0, 101);
if (randomNumber < dropChance)
{
GameObject randomPickup = pickups[Random.Range(0, pickups.Length)];
Instantiate(randomPickup, transform.position, transform.rotation);
}
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
if (dazedTime <= 0)
{
speed = 6;
}
else
{
speed = 0;
dazedTime -= Time.deltaTime;
}
}
private void FixedUpdate()
{
if (playerPos != null)
{
if (Vector2.Distance(transform.position, playerPos.position) > stopDistance && speed > 0)
{
anim.SetBool("isIdle", false);
transform.position = Vector2.MoveTowards(transform.position, new Vector2(playerPos.position.x, transform.position.y), speed * Time.deltaTime);
}
else
{
speed = 0;
}
}
if (knockedback == true)
{
moveDirection = this.transform.position - playerPos.transform.position;
rb.AddForce(moveDirection.normalized * knockback);
rb.velocity = Vector2.ClampMagnitude(rb.velocity.normalized, knockback);
}
}
public void TakeDamage(int damage)
{
dazedTime = startDazedTime;
health -= damage;
hurt.Play();
StartCoroutine(isHit());
StartCoroutine(Knockback());
Debug.Log("damage TAKEN");
}
public void HurtPlayer()
{
playerPos.GetComponent().TakeDamage(damage);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Hit Detection")
{
playerPos.GetComponent().TakeDamage(damage);
}
}
IEnumerator isHit()
{
hit = true;
yield return new WaitForSeconds(0.08f);
hit = false;
}
IEnumerator Knockback()
{
knockedback = true;
yield return new WaitForSeconds(0.01f);
knockedback = false;
}
}
public class EnemyMelee : MonoBehaviour {
private float timeBtwAttack;
public float startTimeBtwAttack;
public Transform attackPos;
public float attackRange;
public int damage;
public float dashForce;
private float maxDashForce;
private Transform playerPos;
public LayerMask whatIsPlayer;
private Vector2 moveDirection;
[HideInInspector]
public bool isDashing;
Enemy enemy;
Rigidbody2D rb;
private void Start()
{
playerPos = GameObject.FindGameObjectWithTag("Player").transform;
GetComponent();
enemy = GetComponent();
}
private void FixedUpdate()
{
GetComponent();
if (timeBtwAttack <= 0)
{
isDashing = true;
timeBtwAttack = startTimeBtwAttack;
Collider2D[] playerToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsPlayer);
for (int i = 0; i < playerToDamage.Length; i++)
{
rb = GetComponent();
moveDirection = this.transform.position - playerPos.transform.position;
Vector2 velocity = rb.velocity;
velocity.x = (moveDirection.normalized * dashForce).x;
rb.velocity = velocity;
}
}
else
{
isDashing = false;
timeBtwAttack -= Time.deltaTime;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
}
}
↧