Can’t damage an enemy shooting from the right. 2D Platformer.

Updated on April 3, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on April 3, 2017

Hello,

So I’ve been having this problem for a long time and it’s the only thing breaking my game. I’ve looked through my code many times and found nothing out of the ordinary. Whenever I am to the right side of the enemy and I try to shoot it, it doesn’t do any damage but it works just fine when I shoot on it’s left side. I tried making it so the “front” of the collider would flip towards the player at all times but that still didn’t work and shouldn’t matter anyways. I have no idea why my colliders are working the way they are.

Video of issue.

Weapon Script:

using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
 public float fireRate = 0;
 public int Damage = 10;
 public LayerMask whatToHit;
 public Transform BulletTrailPrefab;
 float timeToSpawnEffect = 0;
 public float effectSpawnRate = 10;
 float timeToFire = 0;
 public Transform firePoint;
 // Use this for initialization
 void Awake () {
 //find firepoint
 firePoint = transform.FindChild ("FirePoint");
 if (firePoint == null) {
 Debug.LogError ("No firePoint?");
 }
 }
 // Update is called once per frame
 void Update () {
 //regular shooting
 if (fireRate == 0) {
 if (Input.GetButtonDown ("Fire1")) {
 Shoot();
 }
 }
 //Firerate shooting
 else {
 if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
 timeToFire = Time.time + 1/fireRate;
 Shoot();
 }
 }
 }
 void Shoot () {
 //get mouse pos on screen
 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);
 //spawn effect
 if (Time.time >= timeToSpawnEffect) {
 Effect ();
 timeToSpawnEffect = Time.time + 1/effectSpawnRate;
 }
 //draw debug line
 Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
 if (hit.collider != null) {
 Debug.DrawLine (firePointPosition, hit.point, Color.red);
 Enemy enemy = hit.collider.GetComponent();
 if (enemy != null) {
 enemy.DamageEnemy (Damage);
 //Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
 }
 }
 }
 //effect function
 void Effect () {
 Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
 }
}
 

Enemy.cs Script:

using UnityEngine;
using System.Collections;
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 void Init(){
 curHealth = maxHealth;
 }
 }
 public EnemyStats stats = new EnemyStats();
 [Header("Optional: ")]
 [SerializeField]
 private StatusIndicator statusIndicator;
 void Start(){
 stats.Init ();
 if (statusIndicator != null) {
 statusIndicator.SetHealth (stats.curHealth, stats.maxHealth);
 }
 }
 public void DamageEnemy(int Damage){
 stats.curHealth -= Damage;
 if (stats.curHealth <= 0) {
 gameMaster.KillEnemy (this);
 }
 if (statusIndicator != null) {
 statusIndicator.SetHealth (stats.curHealth, stats.maxHealth);
 }
 }
}
 

I have no idea what’s going on and any help would be appreciated. Thank you.

  • Liked by
Reply