Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RayzaN · Nov 09, 2014 at 06:41 PM · collisionplayerdestroyenemy

Player not getting destroyed when touched by enemies

Hello! I'm trying to develop my first 2D shooter. I have 3 types of enemies and 3 types of spells which destroys one of the enemies, but if it hits a wrong enemy type just the projectile gets destroyed.

I've been trying to include a destroy script for when the player gets touched by an enemy, but for some reason I can't get it going.

Here is the script for enemy prefab:

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour {

 public float EnemySpeed;
 private Transform myTransform;

 void Start () {
     myTransform = transform;
 }
 
 void Update () {
     float amtToMove = EnemySpeed * Time.deltaTime;
     myTransform.Translate(Vector3.left * amtToMove);
     
     if (myTransform.position.x < -10.9f)
         Destroy(this.gameObject);
 }
     
 void OnTriggerEnter(Collider otherObject)
 {
     if (otherObject.tag == "Wizzard") {
         Destroy (otherObject.gameObject);
         Destroy (gameObject);
         }
 }

}

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by barjed · Nov 09, 2014 at 06:44 PM

Have you added Colliders and a RigidBody to the object?

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 RayzaN · Nov 09, 2014 at 07:53 PM 0
Share

I have Colliders and RigidBody to all 3 objects.

avatar image
0

Answer by JazzWolf · Nov 09, 2014 at 07:38 PM

This is the enemy script and you want it to destroy the player? In you're onTriggerEnter script you're checking for the tags "EnemyArcane" "EnemyFire" and "EnemyFrost", but theres no checking for the player. I'm guessing the player doesn't have these tags. I think you need to change those to the players tag and make sure your player has the right tag. like this

EDIT: for 2D colliders your probably want to use the 2D physics variations

 void OnTriggerEnter2D(Collider2D otherObject)
  {
      if (otherObject.tag == "PlayerTag") {
          Destroy (otherObject.gameObject);
          Destroy (gameObject);
          }
 }


It might be worth using the OnTriggerEnter and destroy on the player script itself. It seems you're just losing track of your logic a little bit. Let me know if this helps or if you can expand on the problem.

Comment
Add comment · Show 3 · 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 RayzaN · Nov 09, 2014 at 07:53 PM 0
Share

Yeah, sorry I copied the wrong code, the Player has the tag "Wizzard", I just tried to do the other way around, the Player to destroy itself when it gets touched by an enemy but didn't work as well.

avatar image JazzWolf · Nov 10, 2014 at 12:55 AM 0
Share

Wait a sec.. this is a 2D game correct? You are most likely using BoxCollider2D? I'm not positive it matters, but you may need to use OnTriggerEnter2D(Collider2D otherObject).

Also this is probably done already but just in case make sure you have checked the Is Trigger box on the Collider2D's.

avatar image RayzaN · Nov 10, 2014 at 06:16 AM 0
Share

Yes, it is a 2D, but the "Player" is a cube, the "enemies" are capsules and the "projectiles" are spheres. As I said, I managed to get projectiles and enemies work with each other, but using the same settings and different parameters I can't get the capsules interact with the cube, and I made sure the colider area is large enough on the cube. I have is Trigger checked on the enemies.

avatar image
0

Answer by SnotE101 · Nov 10, 2014 at 03:05 AM

Is at least one collider in this collision a trigger?

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 RayzaN · Nov 10, 2014 at 06:17 AM 0
Share

Positive, the enemies are checked.

avatar image
0

Answer by RayzaN · Nov 10, 2014 at 04:25 PM

I managed to solve it, apparently the player had to be a prefab as well. Thanks for your help guys! Have a nice day! :)

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision Detection Issue with SetActive(false) not working 2 Answers

Player Dies when Collide with enemy 2 Answers

Only targeting the targets root of vector 3 and not colliding with its mesh 1 Answer

Destroy Enemy Rigidbody after Jumping on Head? 3 Answers

Player and enemy collision 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