Unity 2d attack animation killing enemy,Sword collision with enemy 2d
Hi, I am fairly new to unity and would like to know how to kill an enemy with a sword. Using the gothicvania asset from unity for my game project. I have scripted for character and enemy and I run into the problem of killing the enemy with a sword. The way I tried was creating a child object and attaching to a player, then I applied circle collider and keyframed the collider only to be active when attacking. In end, it works but I can also kill the player just my running into which is something I don't want and instead I want the player to die if they run or jump into the enemy and can only kill an enemy when attacking with the sword. Not sure where the issue with the script is and would like some sort of guidance or hint towards whats the issue. The three scripts below are for my player, enemy and sword
public class Ghost : MonoBehaviour { // Start is called before the first frame update // public Animator anim; void Start() {
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("Player"))
{
// anim.SetBool("IsDead", true);
Destroy(col.gameObject);
Destroy(gameObject);
//col.gameObject.SetActive(false);
}
}
}
ublic class Player : MonoBehaviour
{
Rigidbody2D rb;
// Animator anim;
public float jumpforce;
public bool grounded;
float dirX, moveSpeed;
private Player player;
int healthPoints = 3;
bool isHurting, isDead;
bool facingRight = true;
Vector3 localScale;
float horizontalMove = 0f;
private Animator anim;
public CharacterController controller;
public float runSpeed = 40f;
private bool faceRight = true;
public Transform groundCheck;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
localScale = transform.localScale;
moveSpeed = 5f;
//player = gameObject.GetComponentInParent<Player>(
}
// Update is called once per frame
void Update()
{
dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;
if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
{
rb.AddForce(Vector2.up * 320f);
}
if (Input.GetKey(KeyCode.DownArrow) && Input.GetKeyDown(KeyCode.DownArrow))
{
anim.SetBool("IsCrouching", true);
anim.SetBool("IsRunning", false);
anim.SetBool("IsJumping", false);
}
else
{
anim.SetBool("IsCrouching", false);
}
if (Input.GetKeyDown(KeyCode.G))
{
anim.SetTrigger("IsAttacking");
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
anim.SetBool("IsRunning", true);
}
else
{
anim.SetBool("IsRunning", false);
}
if (rb.velocity.y == 0)
{
anim.SetBool("IsJumping", false);
}
if (rb.velocity.y > 0)
{
anim.SetBool("IsJumping", true);
}
if (rb.velocity.y < 0)
{
anim.SetBool("IsJumping", false);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag.Equals("ghost"))
{
Destroy(col.gameObject);
Destroy(gameObject);
//col.gameObject.SetActive(false);
}
if (col.gameObject.tag.Equals("Spike"))
{
// Destroy(col.gameObject);
Destroy(gameObject);
anim.SetBool("IsDead", true);
}
if (col.gameObject.name.Equals("Spike") && healthPoints > 0)
{
anim.SetTrigger("IsHurting");
StartCoroutine("Hurt");
}
}
IEnumerator Hurt()
{
isHurting = true;
rb.velocity = Vector2.zero;
if (faceRight)
rb.AddForce(new Vector2(-200f, 200f));
else
rb.AddForce(new Vector2(200f, 200f));
yield return new WaitForSeconds(0.5f);
isHurting = false;
}
private void FixedUpdate()
{
rb.velocity = new Vector2(dirX, rb.velocity.y);
}
private void LateUpdate()
{
if (dirX > 0)
{
faceRight = true;
}
else if (dirX < 0)
{
faceRight = false;
}
if (((faceRight) && (localScale.x < 0)) || ((!faceRight) && (localScale.x > 0)))
localScale.x *= -1;
transform.localScale = localScale;
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player_attack : MonoBehaviour { // Start is called before the first frame update private float timeBtwAttack; private float startTimeBtwAttack; public Transform attackPos; public float attackRange; public LayerMask whatIsEnemies; public int damage; public Animator playerAnim; void Start() {
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("ghost"))
{
// Destroy(col.gameObject);
//Destroy(gameObject);
//col.gameObject.SetActive(false);
}
}
private void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "ghost")
{
Destroy(col.gameObject);
}
}
}
,Hi, I am fairly new to unity and am stuck on script regarding player killing an enemy with the sword. I have a child class object, applied circle collider and along with the script to interact with the enemy sprite. The problem I run into is that I can either kill the enemy in attack animation or just running into it. I am not fine with running into but fine with killing it using an attack, instead, I want to the player to die if they touch the enemy. I have keyframed the box collider on the child class and adding the script but I don't know exactly where I am going wrong. I would like some sort of guidance if possible on the best way to solve this issue. Using gothicvania asset from unity for my game. So far I have movement and everything else down except for kill enemy when attacking.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player_attack : MonoBehaviour { // Start is called before the first frame update private float timeBtwAttack; private float startTimeBtwAttack; public Transform attackPos; public float attackRange; public LayerMask whatIsEnemies; public int damage; public Animator playerAnim; void Start() {
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("ghost"))
{
// Destroy(col.gameObject);
//Destroy(gameObject);
//col.gameObject.SetActive(false);
}
}
private void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "ghost")
{
Destroy(col.gameObject);
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb;
// Animator anim;
public float jumpforce;
public bool grounded;
float dirX, moveSpeed;
private Player player;
int healthPoints = 3;
bool isHurting, isDead;
bool facingRight = true;
Vector3 localScale;
float horizontalMove = 0f;
private Animator anim;
public CharacterController controller;
public float runSpeed = 40f;
private bool faceRight = true;
public Transform groundCheck;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
localScale = transform.localScale;
moveSpeed = 5f;
//player = gameObject.GetComponentInParent<Player>(
}
// Update is called once per frame
void Update()
{
dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;
if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
{
rb.AddForce(Vector2.up * 320f);
}
if (Input.GetKey(KeyCode.DownArrow) && Input.GetKeyDown(KeyCode.DownArrow))
{
anim.SetBool("IsCrouching", true);
anim.SetBool("IsRunning", false);
anim.SetBool("IsJumping", false);
}
else
{
anim.SetBool("IsCrouching", false);
}
if (Input.GetKeyDown(KeyCode.G))
{
anim.SetTrigger("IsAttacking");
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
anim.SetBool("IsRunning", true);
}
else
{
anim.SetBool("IsRunning", false);
}
if (rb.velocity.y == 0)
{
anim.SetBool("IsJumping", false);
}
if (rb.velocity.y > 0)
{
anim.SetBool("IsJumping", true);
}
if (rb.velocity.y < 0)
{
anim.SetBool("IsJumping", false);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag.Equals("ghost"))
{
Destroy(col.gameObject);
Destroy(gameObject);
//col.gameObject.SetActive(false);
}
if (col.gameObject.tag.Equals("Spike"))
{
// Destroy(col.gameObject);
Destroy(gameObject);
anim.SetBool("IsDead", true);
}
if (col.gameObject.name.Equals("Spike") && healthPoints > 0)
{
anim.SetTrigger("IsHurting");
StartCoroutine("Hurt");
}
}
IEnumerator Hurt()
{
isHurting = true;
rb.velocity = Vector2.zero;
if (faceRight)
rb.AddForce(new Vector2(-200f, 200f));
else
rb.AddForce(new Vector2(200f, 200f));
yield return new WaitForSeconds(0.5f);
isHurting = false;
}
private void FixedUpdate()
{
rb.velocity = new Vector2(dirX, rb.velocity.y);
}
private void LateUpdate()
{
if (dirX > 0)
{
faceRight = true;
}
else if (dirX < 0)
{
faceRight = false;
}
if (((faceRight) && (localScale.x < 0)) || ((!faceRight) && (localScale.x > 0)))
localScale.x *= -1;
transform.localScale = localScale;
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ghost : MonoBehaviour { // Start is called before the first frame update // public Animator anim; void Start() {
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("Player"))
{
// anim.SetBool("IsDead", true);
Destroy(col.gameObject);
Destroy(gameObject);
//col.gameObject.SetActive(false);
}
}
}
Your answer
Follow this Question
Related Questions
Detect overlapping objects 2D game 0 Answers
Collider with more health wins collision? Please Help! 0 Answers
How to play animations from script. 0 Answers
How i can fix a player controller bug? 0 Answers
What is ContactPoint.point? 0 Answers