Knockback, Trigger and rigidbody
Hey Guys, here is my problem : I have a script "Damage" on my enemies (and eventually on my player to use the same code when he'll be able to attack).
I'm able to apply a force on he's rigidbody to knock him back, but just after the "push" my control are not disabled and so I can modify the knockback movement too much, even avoid it if I keep "moving forward" on the ennemi. Basically, the knockback is ok if I stand still without touching anything... I tried to get the rigidbody kinematic, but that doesn't seem to work...
Here is my Damage script.
using UnityEngine; using System.Collections;
public class Damage : MonoBehaviour {
public int degats;
public string targetTag;
public bool check;
public float rateBump = 300;
//public bool freeze;
void Start()
{
check = false;
//freeze = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == targetTag) {
//freeze = true;
check = true;
other.GetComponent<Health>().takeDamage(degats);
var thisrb = gameObject.GetComponent<Rigidbody2D>();
var rb = other.GetComponent<Rigidbody2D>();
if(other.attachedRigidbody.position.x > thisrb.position.x) {
rb.AddForce(new Vector2(100 * rateBump, 80 * rateBump));
rb.GetComponent<Rigidbody>().isKinematic = true;
//rb.IsAwake();
}else{
rb.AddForce(new Vector2(-100 * rateBump, 80 * rateBump));
rb.GetComponent<Rigidbody>().isKinematic = true;
//rb.IsAwake();
}
}
}
}
I think I should disable my inputs, but I don't really know how to do that, I've got a pretty complicated Input Manager on a separate script, and I guess that it would ruin the idea of using the same script for damaging players Or ennemies... Anyway, I'm kinda lost here as you can see ^^
Any help would be much appreciated !
An idea I guess would be to make the rigidbody asleep for an certain amount of frames, but I just can't call a yield wait for sec inside of this trigger - if method...
Your answer