- Home /
Question by
AlexReverse · Aug 06, 2018 at 05:50 PM ·
damageknockback
Knockback 2D
So i tried to make a knockback for the player when the player is triggered by the spikes but it just looks stupid and the knockback is too big, here is the script for the knockback, any ideas how to make it look better or something? this is how the knockback looks: VIDEO
so this is the knockback part in the player script:
public void Damage(int dmg)
{
curHealth -= dmg;
}
public IEnumerator Knockback(float knockdur, float knockpwr, Vector3 knockdir)
{
float timer = 0;
while( knockdur > timer)
{
timer += Time.deltaTime;
rb2d.velocity = new Vector2(0, 0);
rb2d.AddForce(new Vector3(knockdir.x * -100, knockdir.y + knockpwr, transform.position.z));
}
yield return 0;
}
}
and the spikes script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spikes : MonoBehaviour
{
private player player;
public float x = 0.02f;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<player>();
}
void OnTriggerEnter2D (Collider2D col)
{
if (col.CompareTag("Player"))
{
player.Damage(2);
StartCoroutine(player.Knockback(x, 300, player.transform.position));
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612172322im_/https://answers.unity.com/themes/thub/images/avi.jpg)