- Home /
How to have BulletPrefab do the damage in instead of RayCastHit
I want the bullet prefab do the damage like have the bullet enter the collidision to decrease the enemy Hp and when the OnTriggerEnter use for PlayerInRange but when i shoot the bullet it collide with the enemy and it move, the enemy have two collider box and capsule collider which is for the PlayerInRange and i want the box for the dmage. when i a enter the enemy capsule collider i notice my health decrease instead of the bullet that the enemy instantiate, i use lookAt but when my enemy want to shoot it seem to go in the wrong direction i mean the ShotSpawnPoint, my character not getting to decrease the enemy health because i using a bullet prefab which i don't know how to tag to do the damage cause i using raycast, the bullet shoot before the raycasthit but i don't want the raycast do the damage but the bullet so i don't know what to do here my scripts.
using UnityEngine; using System.Collections;
public class PlayerShooting : MonoBehaviour {
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public GameObject shotPrefab;
public float range = 100f;
public Transform shotSpawn;
private AudioSource playershot;
float timer;
//Ray shootRay;
//RaycastHit shootHit;
int ShootableMask;
void Start ()
{
ShootableMask = LayerMask.GetMask ("Shootable");
playershot = GetComponent < AudioSource> ();
}
void Update ()
{
timer += Time.deltaTime;
if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
{
Shoot ();
}
}
void Shoot ()
{
timer = 0f;
RaycastHit hit;
Bullet ();
Ray ray = new Ray (shotSpawn.position, transform.forward);
if (Physics.Raycast (ray, out hit, range))
{
if (hit.collider.tag == "Enemy")
//EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth> ();
//if (enemyHealth != null)
//{
//enemyHealth.TakeDamge (damagePerShot);
//}
Debug.DrawRay (shotSpawn.position, transform.forward * range, Color.green);
}
}
void Bullet()
{
GameObject shot = Instantiate (shotPrefab, shotSpawn.position, shotSpawn.rotation) as GameObject;;
playershot.Play ();
}
}
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public Slider healthSlider;
bool isDead;
bool damaged;
PlayerMovement playerMovement;
PlayerShooting playershooting;
public GameObject PlayerExplosion;
private AudioSource playerExplosion;
void Start ()
{
playershooting = GetComponent<PlayerShooting> ();
playerMovement = GetComponent<PlayerMovement> ();
currentHealth = startingHealth;
playerExplosion = GetComponent<AudioSource> ();
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
healthSlider.value = currentHealth;
if (currentHealth < 0 && !isDead) {
Death ();
}
}
public void Death () {
isDead = true;
Instantiate (PlayerExplosion, transform.position, transform.rotation);
playerExplosion.Play ();
playerMovement.enabled = false;
playershooting.enabled = false;
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine; using System.Collections;
public class EnemyAttack : MonoBehaviour {
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
bool playerInRange;
float timer;
GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
public Transform shotSpawn;
public GameObject enemyShot;
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth> ();
enemyHealth = GetComponent<EnemyHealth> ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject == player) {
playerInRange = false;
}
}
void Update ()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
Attack ();
}
}
void Attack ()
{
timer = 0f;
Instantiate (enemyShot, shotSpawn.position, shotSpawn.rotation);
if (playerHealth.currentHealth > 0) {
playerHealth.TakeDamage (attackDamage);
}
}
}
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public GameObject enemyExplosion;
CapsuleCollider capsuleCollider;
bool isDead;
void Start ()
{
capsuleCollider = GetComponent<CapsuleCollider> ();
currentHealth = startingHealth;
}
// Update is called once per frame
void Update () {
}
public void TakeDamge(int amount)
{
if (isDead)
return;
currentHealth -= amount;
if (currentHealth < 0) {
Death ();
}
}
void Death ()
{
isDead = true;
capsuleCollider.isTrigger = true;
Instantiate (enemyExplosion, transform.position, transform.rotation);
}
}
using UnityEngine; using System.Collections;
public class EnemyMovement : MonoBehaviour {
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
public float damping;
public float Speed = 10;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent<PlayerHealth> ();
enemyHealth = GetComponent<EnemyHealth> ();
}
// Update is called once per frame
void Update ()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
lookAtPlayer ();
Movement ();
}
}
void lookAtPlayer ()
{
Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
void Movement ()
{
transform.position = Vector3.MoveTowards (transform.position, player.position, Speed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Making collision boxes objects can go through 1 Answer
My enemies stack and do not use gravity 2 Answers
Makign an object collide continously 1 Answer