I have tried various ways to inflict damage and nothing seems to work.... this is my latest attempt:
using UnityEngine;
using System.Collections;
using System;
public class FireBlast : MonoBehaviour
{
public float speed;
private Player player;
private int BaseLayer;
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 50f;
float timer;
Ray2D FirePoint;
RaycastHit2D shootHit;
int shootableMask;
float effectsDisplayTime = 0.2f;
void Start()
{
player = FindObjectOfType();
if (player.transform.localScale.x < 0){
speed = -speed;
transform.localScale = new Vector3(-1f, 1f, 1f);
}
}
void FixedUpdate()
{
GetComponent().velocity = new Vector2(speed, GetComponent().velocity.y);
}
void OnTriggerEnter2D(Collider2D other)
{
// Reset the timer.
timer = 0f;
// Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
FirePoint.origin = transform.position;
FirePoint.direction = transform.forward;
// Perform the raycast against gameobjects on the shootable layer and if it hits something...
if (Physics2D.Raycast(FirePoint.origin, shootHit.point, range, shootableMask, 10f))
{
// Try and find an EnemyHealth script on the gameobject hit.
EnemyHealth enemyHealth = shootHit.rigidbody.GetComponent();
// If the EnemyHealth component exist...
if (enemyHealth != null)
{
// ... the enemy should take damage.
enemyHealth.TakeDamage(damagePerShot, shootHit.point);
}
}
}
}
I am trying to kill enemies with a basic health system.. Please help!
↧