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 /
This question was closed Jun 20, 2014 at 09:51 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by pinkanna · Jun 17, 2014 at 04:37 AM · 2ddamagehealth

Player won't take damage...

Hi all,

I'm VERY new to Unity and I'm having trouble with a tutorial that I have been following.

The tutorial calls for the enemy to damage the player but, my player doesn't take any damage.

EDITING POST TO SHOW ALL SCRIPTS

Hurting the Player http://pixelnest.io/tutorials/2d-game-unity/shooting-2/

Video Ref https://www.youtube.com/watch?v=ePkkRRulME0

Does anyone have an idea of what I could be doing wrong? I went back and redid the enemy shot steps and unticked "isEnemy" on the health script for the player..

HEALTHSCRIPT ON PLAYER

 using UnityEngine;
 
 /// <summary>
 /// Handle hitpoints and damages
 /// </summary>
 public class HealthScript : MonoBehaviour
 {
     /// <summary>
     /// Total hitpoints
     /// </summary>
     public int hp = 1;
     
     /// <summary>
     /// Enemy or player?
     /// </summary>
     public bool isEnemy = true;
     
     /// <summary>
     /// Inflicts damage and check if the object should be destroyed
     /// </summary>
     /// <param name="damageCount"></param>
     public void Damage(int damageCount)
     {
         hp -= damageCount;
         
         if (hp <= 0)
         {
             // Dead!
             Destroy(gameObject);
         }
     }
     
     void OnTriggerEnter2D(Collider2D otherCollider)
     {
         // Is this a shot?
         ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>();
         if (shot != null)
         {
             // Avoid friendly fire
             if (shot.isEnemyShot != isEnemy)
             {
                 Damage(shot.damage);
                 
                 // Destroy the shot
                 Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script
             }
         }
     }
 }



  

Player Script..

 using UnityEngine;
 
 /// <summary>
 /// Player controller and behavior
 /// </summary>
 public class PlayerScript : MonoBehaviour
 {
     /// <summary>
     /// 1 - The speed of the ship
     /// </summary>
     public Vector2 speed = new Vector2(50, 50);
     
     // 2 - Store the movement
     private Vector2 movement;
     
     void Update()
     {
         // 3 - Retrieve axis information
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
         
         // 4 - Movement per direction
         movement = new Vector2(
             speed.x * inputX,
             speed.y * inputY);
 
         // ...
         
         // 5 - Shooting
         bool shoot = Input.GetButtonDown("Fire1");
         shoot |= Input.GetButtonDown("Fire2");
         // Careful: For Mac users, ctrl + arrow is a bad idea
         
         if (shoot)
         {
             WeaponScript weapon = GetComponent<WeaponScript>();
             if (weapon != null)
             {
                 // false because the player is not an enemy
                 weapon.Attack(false);
             }
         }
         
         // ...
 
 
     }
 
     
     void FixedUpdate()
     {
         // 5 - Move the game object
         rigidbody2D.velocity = movement;
     }
 }
 

Weapon Script

 using UnityEngine;
 
 /// <summary>
 /// Launch projectile
 /// </summary>
 public class WeaponScript : MonoBehaviour
 {
     //--------------------------------
     // 1 - Designer variables
     //--------------------------------
     
     /// <summary>
     /// Projectile prefab for shooting
     /// </summary>
     public Transform shotPrefab;
     
     /// <summary>
     /// Cooldown in seconds between two shots
     /// </summary>
     public float shootingRate = 0.25f;
     
     //--------------------------------
     // 2 - Cooldown
     //--------------------------------
     
     private float shootCooldown;
     
     void Start()
     {
         shootCooldown = 0f;
     }
     
     void Update()
     {
         if (shootCooldown > 0)
         {
             shootCooldown -= Time.deltaTime;
         }
     }
     
     //--------------------------------
     // 3 - Shooting from another script
     //--------------------------------
     
     /// <summary>
     /// Create a new projectile if possible
     /// </summary>
     public void Attack(bool isEnemy)
     {
         if (CanAttack)
         {
             shootCooldown = shootingRate;
             
             // Create a new shot
             var shotTransform = Instantiate(shotPrefab) as Transform;
             
             // Assign position
             shotTransform.position = transform.position;
             
             // The is enemy property
             ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
             if (shot != null)
             {
                 shot.isEnemyShot = isEnemy;
             }
             
             // Make the weapon shot always towards it
             MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
             if (move != null)
             {
                 move.direction = this.transform.right; // towards in 2D space is the right of the sprite
             }
         }
     }
     
     /// <summary>
     /// Is the weapon ready to create a new projectile?
     /// </summary>
     public bool CanAttack
     {
         get
         {
             return shootCooldown <= 0f;
         }
     }
 }

Enemy Script

 using System.Collections.Generic;
 using UnityEngine;
 
 /// <summary>
 /// Enemy generic behavior
 /// </summary>
 public class EnemyScript : MonoBehaviour
 {
     private WeaponScript[] weapons;
     
     void Awake()
     {
         // Retrieve the weapon only once
         weapons = GetComponentsInChildren<WeaponScript>();
     }
     
     void Update()
     {
         foreach (WeaponScript weapon in weapons)
         {
             // Auto-fire
             if (weapon != null && weapon.CanAttack)
             {
                 weapon.Attack(true);
             }
         }
     }
 }


Shot Script

 using UnityEngine;
 
 /// <summary>
 /// Projectile behavior
 /// </summary>
 public class ShotScript : MonoBehaviour
 {
     // 1 - Designer variables
     
     /// <summary>
     /// Damage inflicted
     /// </summary>
     public int damage = 1;
     
     /// <summary>
     /// Projectile damage player or enemies?
     /// </summary>
     public bool isEnemyShot = false;
     
     void Start()
     {
         // 2 - Limited time to live to avoid any leak
         Destroy(gameObject, 20); // 20sec
     }
 }

Comment
Add comment · Show 1
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 bekiramina1994 · Sep 13, 2017 at 04:26 AM 0
Share

@pinkanna what you did to solve this problem ?, tell me please

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Pyrian · Jun 17, 2014 at 05:11 AM

There's a lot that might be going wrong elsewhere, but what jumps out to me is:

 if (shot.isEnemyShot != isEnemy)

Now, isEnemy is a Boolean set to true. So, now you're asking if "isEnemyShot" is NOT equal to true - equivalent to checking if it's false. So, you don't take damage unless "isEnemyShot" is false. In all likelihood, this means you can only shoot yourself, and you can't get shot by your enemies.

Maybe just change it to:

 if (shot.isEnemyShot == isEnemy)
Comment
Add comment · Show 14 · 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 pinkanna · Jun 17, 2014 at 11:41 AM 0
Share

Thanks for responding. I will check this when I get home.

avatar image pinkanna · Jun 17, 2014 at 08:04 PM 0
Share

Hey, I just tested that change. Now when I shoot the player disappears. The player only disappears when I shoot... should I post other scripts? Like my shot script? or enemy script? Thanks..

avatar image Pyrian · Jun 17, 2014 at 10:59 PM 0
Share

Heh. Yeah, maybe post all the scripts.

avatar image pinkanna · Jun 18, 2014 at 10:02 AM 0
Share

Edited post to show all scripts...thank you again

avatar image tanoshimi · Jun 18, 2014 at 11:46 AM 0
Share

@GrahamDunnett - can you ban user @honestgirl23 please - they're clearly a bot and I've been deleting these repeating comments as they come in, but need permenent ban.

Show more comments

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

2D flash health and damage system 0 Answers

How to create/fix fire damage script???? 1 Answer

Damage script is screwed up...? what to do? 1 Answer

Damage not triggering in build 1 Answer

Ai that applies damage in 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