- Home /
The question is answered, right answer was accepted
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
}
}
@pinkanna what you did to solve this problem ?, tell me please
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)
Thanks for responding. I will check this when I get home.
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..
@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.
Follow this Question
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