Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 IsaacHoward · Dec 10, 2015 at 12:48 AM · c#unity 5collisiondestroy

how do i collide and kill the enemy while pressing space Unity 5 C#

using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

 private Rigidbody2D rbody;
 private Animator anim;
 public bool isAttacking = false;

 // Use this for initialization
 void Start () {
     rbody = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void Update () {
     Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal") , Input.GetAxisRaw("Vertical"));


     //if not zero must be moving
     if (movement_vector != Vector2.zero)
     {
         anim.SetBool("isWalking", true);
         anim.SetFloat("input_x", movement_vector.x);
         anim.SetFloat("input_y", movement_vector.y);
     }
     else //sees if in idel or not changes anim
     {
         anim.SetBool("isWalking", false);

     }
     //movment by 1
     rbody.MovePosition(rbody.position + movement_vector * Time.deltaTime);

     //sets attack to true if space hit
     if (Input.GetKeyDown(KeyCode.Space))
     {
         isAttacking = true;
        
     }
     //Makes anim attack
     if (isAttacking == true)
     {
         anim.SetBool("isAttacking", true);
         isAttacking = false;
        
     }
     else
     {
         anim.SetBool("isAttacking", false);
     }


 }

 //checks if touch enemy and then if can attack then kill
 void OnTriggerEnter2D(Collider2D other)
 {

     if (other.gameObject.CompareTag("Enemy"))
     {
         if (isAttacking==true)
         {
             Destroy(other.gameObject);
         }
     }
 }

}//end

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nick_bain · Dec 10, 2015 at 02:10 AM

@username, in the OnTriggerEnter2D() function, instead of:

if(other.gameobject.CompareTag("Enemy")){ //Do stuff }

Just use:

if(other.tag == "Enemy"){ //Do stuff }

Comment
Add comment · Show 1 · Share
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
avatar image IsaacHoward · Dec 10, 2015 at 02:17 AM 0
Share

Thank you but That didn't help i want to hit space and attack while i am touching the enemy to destroy it. But all this script does currently is i have to be attacking and moving into collision at the same time to destroy the enemy.

avatar image
0

Answer by nick_bain · Dec 10, 2015 at 03:53 AM

Ok, so to do that:

  1. Make a new float called: enemyDistance ----Code>>>> public float enemyDistance

  2. In Update(), put: enemyDistance=Vector3.Distance(enemy.transform.position,transform.position);

  3. In OnTriggerEnter2D(), type in (mostly add a little bit more info):

void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Enemy") { if (isAttacking==true && enemyDistance <= 5) <<< If you want the distance to be closer or farther, then change the value 5. { Destroy(other.gameObject); } } }

Comment
Add comment · Show 2 · Share
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
avatar image IsaacHoward · Dec 10, 2015 at 04:46 AM 0
Share

i see how this could work but it didn't. i made some adjustments but ran into this error "Non-invocable member ' gameObject.tag' cannot be used like a method. "

$$anonymous$$y new code is Also thanks for trying to help me ^-^

using UnityEngine; using System.Collections;

public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {

 private Rigidbody2D rbody;
 private Animator anim;
 public bool isAttacking = false;
 public float enemyDistance;
 public Rigidbody2D enemy;

 // Use this for initialization
 void Start () {
     rbody = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void Update () {
     Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal") , Input.GetAxisRaw("Vertical"));


     //if not zero must be moving
     if (movement_vector != Vector2.zero)
     {
         anim.SetBool("isWalking", true);
         anim.SetFloat("input_x", movement_vector.x);
         anim.SetFloat("input_y", movement_vector.y);
     }
     else //sees if in idel or not changes anim
     {
         anim.SetBool("isWalking", false);

     }
     //movment by 1
     rbody.$$anonymous$$ovePosition(rbody.position + movement_vector * Time.deltaTime);

     //sets attack to true if space hit
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
     {
         isAttacking = true;
         Debug.Log("Attack");

     }
     //$$anonymous$$akes anim attack
     if (isAttacking == true)
     {
         anim.SetBool("isAttacking", true);
         isAttacking = false;
         Attack();
     }
     else
     {
         anim.SetBool("isAttacking", false);
     }

     enemyDistance = Vector3.Distance(enemy.transform.position, transform.position);




 }

 // checks if touch enemy and then if can attack then kill
 //void OnTriggerEnter2D(Collider2D other)
 //{
 //    {
 //        if (gameObject.tag == "Enemy")
 //        {
 //            Debug.Log("Touch");
 //            if (isAttacking == true && enemyDistance <= 0)
 //            {
 //                Debug.Log("Die");
 //                Destroy(other.gameObject);
 //            }

 //        }
 //    }
 //}

 public void Attack()
 {
     if (GetComponent<Collider2D>(gameObject.tag("Enemy"))) 
     {
         Debug.Log("Touch");
         if (isAttacking == true && enemyDistance<= 0)
         {
             Debug.Log("Die");
             Destroy(gameObject);
         }

     }
 }


}//end

avatar image IsaacHoward · Dec 10, 2015 at 05:07 AM 0
Share

Never $$anonymous$$d i just got it to work thank you for your help it means a lot.

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

52 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

Related Questions

Destroy instantiated Projectile after time 1 Answer

[SOLVED] Problem with OnCollisionEnter2D script after upgrading to Unity 5.3(from Unity 4.6) 1 Answer

Remove Object from Game after Collision with Player (Unity 5.2.3f1) 1 Answer

Destroy moving objcts through touch on screen 0 Answers

Delete object with key within Trigger 1 Answer


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