Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TheOnly1ZombieGamer · Oct 13, 2019 at 01:12 AM · 2d game2d-gameplaymelee

How to fix knockback in 2D melee combat system

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<Rigidbody2D>();
         anim = GetComponent<Animator>();
         attack = GetComponent<EnemyMelee>();
         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<PlayerController>().TakeDamage(damage);
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.name == "Hit Detection")
         {
             playerPos.GetComponent<PlayerController>().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<PlayerController>();
     enemy = GetComponent<Enemy>();
 }

 private void FixedUpdate()
 {
     GetComponent<PlayerController>();
     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<Rigidbody2D>();
             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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

clamp position 1/4 of a circle 2D 1 Answer

i updated my unity 5. and now getting errors 1 Answer

When i flip, the sprite moves 1 Answer

Throwing a sword 2 Answers

How do I keep my player colliders from thinking that the player is in the air. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges