Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Senteial · Mar 31, 2019 at 09:18 AM · scripting problemcollision2d animation

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);

     }
   




 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

292 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges