Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How to handle knockback with large swarms of enemy

$
0
0
A main aspect of my game is it being a horde hack and slash, but I've run into the issue of how do I handle knocking back enemies when they collide with other enemies? my enemies are moved using transform.position, and they are knocked back by having that disabled and applying a force to them. however when they collide with another enemy that stop dead in their tracks, the only thing changing it on surface level being when i set the enemies weight to be super super low but this causes a host of other issues, namely the primary enemy gets launched, and if i turned down the numbers so they don't then i'm back where i started. I can grab certain parts of my code if it would help, but so far I'm mostly asking if anyone has a good idea how to handle this situation? Cheers for any help! Edit: I've added the script that handles basic enemy movement (sorry if my code is a bit of a mess) using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMovement : MonoBehaviour { [SerializeField] private float speed = 1.1f; public bool Stop = false; private GameObject Player; private Rigidbody2D rb; private float moveH, moveV; private Vector2 PlayerLoca; private Collider2D PlayerPrefab; // Start is called before the first frame update void Start() { rb = GetComponent(); Player = GameObject.FindGameObjectWithTag("Player"); } // Update is called once per frame void Update() { } private void FixedUpdate() { if (Stop == false) { WalkTo(); CheckVelocity(); moveH = (Player.transform.position.x - transform.position.x) * -1; moveV = (Player.transform.position.y - transform.position.y) * -1; } } private void CheckVelocity() { Vector2 vec = new Vector2(0, 0); if (rb.velocity != vec) { rb.velocity = vec; } } private void OnTriggerExit2D(Collider2D collider) { if (collider.CompareTag("Player")) { StartCoroutine(WaitToMove()); } } private IEnumerator BounceOff() { yield return new WaitForSeconds(0.1f); rb.velocity = new Vector2(moveH, moveV); yield return new WaitForSeconds(0.5f); rb.velocity = new Vector2(moveH * 0, moveV * 0); } private IEnumerator WaitToMove() { yield return new WaitForSeconds(1); Stop = false; } private void WalkTo() { PlayerLoca = Vector2.Lerp(transform.position, Player.transform.position, 0.05f); transform.position = Vector2.MoveTowards(transform.position, PlayerLoca, speed * Time.deltaTime); } private void OnTriggerEnter2D(Collider2D collider) { if (collider.CompareTag("Player")) { //Stop = true; //StartCoroutine(BounceOff()); //this was the code for making the enemy bounce off of the player upon contact. } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>