- Home /
2D Topdown Shooter Enemy Knockback
Hello! I have a problem.
I'm working on my first ever game, a 2D Topdown Shooter. Since I'm doing this for the first time I'm still very inexperienced with code and have been mostly consulting the internet on any code needs that I have since I don't quite know how to do all things myself yet.
Currently I'm stumped on creating a simple knockback system. The player moves around and shoots the enemies with projectiles, and I want the enemy to get knocked back a bit when he gets hit by said projectile. Right now I have a knockback script attached to the projectile prefab, which you can see here:
public float thrust;
public float knockTime;
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Enemy"))
{
Rigidbody2D enemy = other.GetComponent<Rigidbody2D>();
if(enemy != null)
{
enemy.isKinematic = false;
Vector2 difference = enemy.transform.position - transform.position;
difference = difference.normalized * thrust;
enemy.AddForce(difference, ForceMode2D.Impulse);
StartCoroutine(KnockCo(enemy));
}
}
}
private IEnumerator KnockCo(Rigidbody2D enemy)
{
if(enemy != null)
{
yield return new WaitForSeconds(knockTime);
enemy.velocity = Vector2.zero;
enemy.isKinematic = true;
}
}
But when the enemy gets knocked back he doesn't "recover" and just kind of stutters and can't move properly anymore. If needed I'll provide the enemy and projectile scripts here aswell, just in case:
Enemy:
public float speed;
private Transform playerPos;
private PlayerController player;
public GameObject killeffect;
public float health;
public int scoreValue = 1;
private Shake shake;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
playerPos = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
shake.CamShake();
Instantiate(killeffect, transform.position, Quaternion.identity);
player.health--;
Debug.Log(player.health);
Destroy(gameObject);
SoundManagerScript.PlaySound("hurt");
}
if (other.CompareTag("Projectile"))
{
Destroy(other.gameObject);
health--;
SoundManagerScript.PlaySound("hurt");
}
if (health <= 0)
{
Instantiate(killeffect, transform.position, Quaternion.identity);
Destroy(other.gameObject);
Destroy(gameObject);
ScoreDisplay.score += scoreValue;
SoundManagerScript.PlaySound("enemydeath");
}
}
Projectile:
public float speed;
private Vector2 target;
public GameObject projectileeffect;
void Start()
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, target) < 0.2f)
{
Instantiate(projectileeffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Enemy"))
{
Instantiate(projectileeffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
Enemy Knockback when hit player 2D Topdown 1 Answer
Difficulty with my tank game 2 Answers
C# LookAt Mouse 2D 2 Answers
How do I make my enemy have animations for directions during movemvent 1 Answer
Distribute terrain in zones 3 Answers