Help with 2D topdown knockback
Hello! i am having a problem with my 2D topdown game, i want to make a slime knockback the player, and i can´t do it work like i want to, i want it to knockfrom right to the left, up to down ETC. but it only knocks from right to left and left to right, i have tried many things i will add the playermovement and the hurtplayer script, also the AI script. AI:
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour {
private bool attacking;
public float attackTime;
private float attackTimeCounter;
private Vector3 Player;
private Vector2 Playerdirection;
private float Xdif;
private float Ydif;
private float speed;
private int Wall;
private float distance;
private float distanceFromPlayer;
Rigidbody2D rb;
public float Range = 5;
public float Speed;
Animator anim;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
Wall = 1 << 8;
speed = Speed;
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
distance = Vector2.Distance (Player, transform.position);
Player = GameObject.Find ("Player").transform.position;
if (distance < Range) {
Xdif = Player.x - transform.position.x;
Ydif = Player.y - transform.position.y;
Playerdirection = new Vector2 (Xdif, Ydif);
if (!Physics2D.Raycast (transform.position, Playerdirection, 5, Wall))
{
rb.AddForce (Playerdirection.normalized * speed);
}
}
}
}
player movement
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
Rigidbody2D rbody;
Animator anim;
private bool attacking;
public float attackTime;
private float attackTimeCounter;
public float knockBack;
public float knockBackCount;
public float knockBackLength;
public bool knockFromRight;
public bool knockFromUp;
public bool knockFromLeft;
public bool knockFromDown;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (knockBackCount <= 0) {
Vector2 movement_vector = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
if (movement_vector != Vector2.zero) {
anim.SetBool ("iswalking", true);
anim.SetFloat ("input_x", movement_vector.x);
anim.SetFloat ("input_y", movement_vector.y);
} else {
anim.SetBool ("iswalking", false);
}
rbody.MovePosition (rbody.position + movement_vector * Time.deltaTime);
{
if (Input.GetKeyDown (KeyCode.J)) {
attackTimeCounter = attackTime;
attacking = true;
anim.SetBool ("isattacking", true);
}
if (attackTimeCounter > 0) {
attackTimeCounter -= Time.deltaTime;
}
if (attackTimeCounter <= 0) {
attacking = false;
anim.SetBool ("isattacking", false);
}
}
} else {
if (knockFromRight)
rbody.velocity = new Vector2 (-knockBack, 0f);
else
knockFromRight = false;
if (knockFromLeft)
rbody.velocity = new Vector2 (knockBack, 0f);
else
knockFromLeft = false;
if (knockFromUp)
rbody.velocity = new Vector2 (0f, -knockBack);
else
knockFromUp = false;
if (knockFromDown)
rbody.velocity = new Vector2 (0f, knockBack);
else
knockFromDown = false;
knockBackCount -= Time.deltaTime;
}
if (knockBackCount <= 0)
{
knockFromRight = false;
knockFromLeft = false;
knockFromUp = false;
knockFromDown = false;
knockBackCount = 0;
}
}
}
hurtplayer
using UnityEngine;
using System.Collections;
public class HurtPlayer : MonoBehaviour {
public int damageToGive;
public GameObject damageNumber;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.name == "Player") {
other.gameObject.GetComponent<PlayerHealthManager> ().HurtPlayer (damageToGive);
var clone = (GameObject)Instantiate (damageNumber, other.transform.position, Quaternion.Euler (Vector3.zero));
clone.GetComponent<FloatingNumbers> ().damageNumber = damageToGive;
}
}
void OnTriggerEnter2D(Collider2D other)
{
var player = other.GetComponent<PlayerMovement> ();
player.knockBackCount = player.knockBackLength;
if (other.gameObject.name == "Player") {
if (other.transform.position.x < transform.position.x)
player.knockFromRight = true;
if (other.transform.position.x > transform.position.x)
player.knockFromLeft = true;
if (other.transform.position.y < transform.position.y)
player.knockFromUp = true;
if (other.transform.position.y > transform.position.y)
player.knockFromDown = true;
}
if (player.knockFromRight == true)
{
player.knockFromDown = false;
player.knockFromUp = false;
player.knockFromLeft = false;
}
if (player.knockFromLeft == true)
{
player.knockFromDown = false;
player.knockFromUp = false;
player.knockFromRight = false;
}
if (player.knockFromUp == true)
{
player.knockFromDown = false;
player.knockFromRight = false;
player.knockFromLeft = false;
}
if (player.knockFromDown == true)
{
player.knockFromRight = false;
player.knockFromUp = false;
player.knockFromLeft = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Knockback problem 2 Answers
Arkanoid-like bounce troubles 1 Answer
How do I make Polygon Collider 2D more smooth? 0 Answers
Trying to add force to an object... is this done correctly? 0 Answers
How to make a knocback smooth - 2D Unity 0 Answers