Im reaching out to anyone here who has a friendly discord or youtube channel that i can learn this from. However tutorials ive seen on youtube are years old and dont work well for what im trying to achieve. I already have my character able to hit the enemy and them to hit the player fine and deal damage thus reducing my health bar but just wanted to jazz that up more.
the style of knock back im trying to figure out for when i land on an enemy or getting hit by them would be like that of Castlevania or even maybe Megaman Knock back style enemy hit.
Currently im still researching other ways. Thanks in advance and tips or examples to share
I took a whack at a knockback… I used an AnimationCurve to knock the player back. Full package enclosed.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker
// cheap and cheerful knockback
// To use:
// put on the player
// set up the animation curve!!!!!
// call DoKnockback() when you want a knockback
// This is for 3D. see comment below for 2D.
//
public class Knockback : MonoBehaviour
{
[Header( "Be sure to fill this with some kind of curve!", order = 1)]
[Header( "Fill this with actual time and speed back.", order = 2)]
public AnimationCurve KnockBackCurve;
float time;
bool requested;
// call this to knock back
public void DoKnockback()
{
requested = true;
}
void Update ()
{
// requested knockback?
if (requested)
{
// performed request
requested = false;
// nonzero time starts sequence
time = Time.deltaTime;
}
// knockback going on?
if (time > 0)
{
float y = KnockBackCurve.Evaluate( time);
time += Time.deltaTime;
if (time >= KnockBackCurve.keys[ KnockBackCurve.keys.Length - 1].time)
{
time = 0; // done
}
// knockback direction is negative Z... this is for 3D
// for 2D you might use negative X?
Vector3 direction = -transform.forward;
// if you use Rigidbody, always use rigidbody.MovePosition()!
transform.position += direction * y * Time.deltaTime;
}
}
}
8413701–1112274–Knockback.unitypackage (6.69 KB)
Thank you. Highly appreciated. I will pick this apart and study it for what i need