- Home /
Question by
Lucario3013 · May 17, 2020 at 06:00 AM ·
2dplatformerlocalscaleknockback
Trying to make my Player's sword knockback the enemy without making the enemy animation freak out.
I have a patrol type enemy for my 2D platformer and I am trying to make the enemy knock back when I hit it with a sword. Right now, I am using AddForce, but because of the way my Enemy movement is setup, its localScale and moveRight gets messed up causing the Enemy to flip really fast between scale x = 1 and -1. I need help finding a work around for this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHit : MonoBehaviour
{
public int meleeDamage;
public int thrust;
private PlayerController Player;
public GameObject enemyDeathEffect;
public GameObject hitEffect;
// Start is called before the first frame update
void Start()
{
Player = GetComponentInParent<PlayerController>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
Rigidbody2D enemy = collision.GetComponent<Rigidbody2D>();
if(enemy != null)
{
enemy.AddForce(new Vector2(0, 1) * thrust, ForceMode2D.Impulse);
}
Instantiate(enemyDeathEffect, collision.transform.position, collision.transform.rotation);
collision.gameObject.GetComponent<EnemyHealth>().TakeDamage(meleeDamage);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour
{
public float moveSpeed;
public bool moveRight;
private Rigidbody2D myRigidbody;
private Animator anim;
private EnemyController enemyController;
public Transform wallDetection;
private float wallRadius = 0.01f;
public LayerMask whatIsWall;
private bool hittingWall;
private bool notAtEdge;
public Transform edgeDetection;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
enemyController = GetComponent<EnemyController>();
}
// Update is called once per frame
void Update()
{
WallCheck();
EdgeCheck();
if (hittingWall || !notAtEdge)
moveRight = !moveRight;
MoveEnemy();
}
public void MoveEnemy()
{
if (myRigidbody.velocity != Vector2.zero)
anim.SetBool("walk", true);
else
anim.SetBool("walk", false);
if (moveRight)
{
myRigidbody.velocity = new Vector3(moveSpeed, myRigidbody.velocity.y);
transform.localScale = new Vector2(1, 1);
}
else
{
myRigidbody.velocity = new Vector3(-moveSpeed, myRigidbody.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
}
void WallCheck()
{
Vector2 wallDetect = new Vector2(wallDetection.position.x, wallDetection.position.y);
hittingWall = Physics2D.OverlapCircle(wallDetect, wallRadius, whatIsWall);
}
void EdgeCheck()
{
Vector2 edgeDetect = new Vector2(edgeDetection.position.x, edgeDetection.position.y);
notAtEdge = Physics2D.OverlapCircle(edgeDetect, wallRadius, whatIsWall);
}
}
Comment
Your answer
Follow this Question
Related Questions
animation 2d platformer 2 Answers
issue with the rigid body x axis ,addforce doesn't work as it should for x axis 1 Answer
Cinemachine camera weird movement on Windows build 0 Answers
2D Flashlight for a platformer 2 Answers
Velocity from external objects 0 Answers