2D Platformer Enemy not Dying and No camera Shake!

Updated on November 1, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on October 19, 2017

Hi there i have a really bad problem as my player can not kill my enemy. As soon as the enemy gets to 10 hp it just doesn’t die. My camera shake doesn’t work and it keeps highlighting “camShake.Shake(camShakeAmt, camShakeLength);” Please help. Also the player health doesn’t go down either. I have posted my code please help!!! It was fine for a bit then just went crazy!

Player scripts:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

[System.Serializable]
public class PlayerStats
{
public int maxHealth = 100;

private int _curHealth;
public int curHealth
{
get { return _curHealth; }
set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
}

public void Init()
{
curHealth = maxHealth;
}

}

public PlayerStats stats = new PlayerStats();

public int fallBoundary = -20;

[SerializeField]
private StatusIndicator statusIndicator;

void Start()
{
stats.Init();

if(statusIndicator == null)
{
Debug.LogError(“No status indicator”);
}
else
{
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}
}

void Update()
{
if(transform.position.y <= fallBoundary)
{
DamagePlayer(9999999);
}
}

public void DamagePlayer(int damage)
{
stats.curHealth -= damage;
if(stats.curHealth <= 0)
{
GameMaster.KillPLayer(this);
}

statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);

}

}

 

Weapon :

using System.Collections;
using UnityEngine;

public class Weapon : MonoBehaviour {

public float fireRate = 0;
public int Damage = 10;
public LayerMask WhatToHit;

public Transform BulletTrailPrefab;
public Transform HitPrefab;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 0;
public Transform MuzzelFlashPrefab;
float timeToFire = 0;
Transform firePoint;

CameraShake camShake;

//Handle camera shaking
public float camShakeAmt = 0.05f;
public float camShakeLength = 0.1f;

// Use this for initialization
void Awake() {
firePoint = transform.Find(“FirePoint”);
if (firePoint == null)
{
Debug.LogError(“No firePoint”);
}

}

private void Start()
{
camShake = GameMaster.gm.GetComponent<CameraShake>();
if(camShake == null)
{
Debug.LogError(“No Camera Shake script found on GM object.”);
}
}

// Update is called once per frame
void Update() {
if (fireRate == 0)
{
if (Input.GetButtonDown(“Fire1”))
{
Shoot();
}
}
else
{
if (Input.GetButton(“Fire1”) && Time.time > timeToFire)
{
timeToFire = Time.time + 1 / fireRate;
Shoot();
}
}
}

void Shoot()
{
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition – firePointPosition, 100, WhatToHit);

Debug.DrawLine(firePointPosition, (mousePosition – firePointPosition) * 100, Color.cyan);
if (hit.collider != null)
{
Debug.DrawLine(firePointPosition, hit.point, Color.red);
Debug.Log(“We hit ” + hit.collider.name + ” and did ” + Damage + ” damage”);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if(enemy != null)
{
enemy.DamageEnemy(Damage);
}
}

if (Time.time >= timeToSpawnEffect)
{
Vector3 hitPos;
Vector3 hitNormal;

if (hit.collider == null)
{
hitPos = (mousePosition – firePointPosition) * 30;
hitNormal = new Vector3(9999, 9999, 9999);
}
else
{
hitPos = hit.point;
hitNormal = hit.normal;
}
Effect(hitPos, hitNormal);
timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
}
}

void Effect(Vector3 hitPos, Vector3 hitNormal)
{
Transform trail = Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation) as Transform;
LineRenderer lr = trail.GetComponent<LineRenderer>();

if (lr != null)
{
lr.SetPosition(0, firePoint.position);
lr.SetPosition(1, hitPos);
//Set Position
}

Destroy(trail.gameObject, 0.04f);

if (hitNormal != new Vector3(9999, 9999, 9999))
{
Transform hitParticle = Instantiate(HitPrefab, hitPos, Quaternion.FromToRotation(Vector3.forward, hitNormal)) as Transform;
Destroy(hitParticle.gameObject, 1f);
}

Transform clone = Instantiate(MuzzelFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
clone.parent = firePoint;
float size = Random.Range(0.6f, 0.9f);
clone.localScale = new Vector3(size, size, size);
Destroy(clone.gameObject, 0.02f);

//Shake Camera
camShake.Shake(camShakeAmt, camShakeLength);
}

}

 

Enemy:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {

[System.Serializable]
public class EnemyStats
{
public int maxHealth = 100;

private int _curHealth;
public int curHealth
{
get { return _curHealth; }
set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
}

public int damage = 40;

public void Init()
{
curHealth = maxHealth;
}

}

public float shakeAmount = 0.1f;
public float shakeLength = 0.1f;

public EnemyStats stats = new EnemyStats();

public Transform deathParticles;

[Header(“Optional: “)]
[SerializeField]
private StatusIndicator statusIndicator;

void Start()
{
stats.Init();

if(statusIndicator != null)
{
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}

if(deathParticles == null)
{
Debug.LogError(“No death Particles”);
}

}

public void DamageEnemy(int damage)
{
stats.curHealth -= damage;
if (stats.curHealth <= 0)
{
GameMaster.KillEnemy(this);
}

if (statusIndicator != null)
{
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}
}

private void OnCollisionEnter2D(Collision2D _colInfo)
{
Player _player = _colInfo.collider.GetComponent<Player>();
if(_player == null)
{
_player.DamagePlayer(stats.damage);
DamageEnemy(9999);
}
}

}

 

GameMaster:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMaster : MonoBehaviour {

public static GameMaster gm;

void Awake()
{
if(gm == null)
{
gm = GameObject.FindGameObjectWithTag(“GM”).GetComponent<GameMaster>();
}
}

public Transform playerPrefab;
public Transform spawnPoint;
public int spawnDelay = 2;
public Transform spawnPrefab;
public AudioClip respawnAudio;

public CameraShake cameraShake;

private void Start()
{
if(cameraShake == null)
{
Debug.LogError(“No camerashake “);
}
}

public IEnumerator RespawnPlayer ()
{
AudioSource.PlayClipAtPoint(respawnAudio, new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z), 5f);
yield return new WaitForSeconds(spawnDelay);

Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
Transform clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as Transform;
Destroy(clone.gameObject, 3f);
}

public static void KillPLayer(Player player)
{
Destroy(player.gameObject);
gm.StartCoroutine (gm.RespawnPlayer());
}

public static void KillEnemy(Enemy enemy)
{
gm._KillEnemy(enemy);
}

public void _KillEnemy(Enemy _enemy)
{
Transform _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as Transform;
Destroy(_clone.gameObject, 5f);
cameraShake.Shake(_enemy.shakeAmount, _enemy.shakeLength);
Destroy(_enemy.gameObject);
}

}

  • Liked by
Reply
0 on November 1, 2017

Same problem here 🙁 please help

  • Liked by
Reply
Cancel
0 on November 1, 2017

Problem solve:

Change:

public void _KillEnemy(Enemy _enemy)
{
Transform _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as Transform;
Destroy(_clone.gameObject, 5f);
cameraShake.Shake(_enemy.shakeAmount, _enemy.shakeLength);
Destroy(_enemy.gameObject);
}

 

For:

public void _KillEnemy(Enemy _enemy)
{
Transform _clone = Instantiate(_enemy.deathParticles, _enemy.transform.position, Quaternion.identity) as Transform;
Destroy(_clone.gameObject, 5f);
Destroy(_enemy.gameObject);
cameraShake.Shake(_enemy.shakeAmount, _enemy.shakeLength);
}

  • Liked by
Reply
Cancel