- Home /
[Answered](C#) Help with dealing damage to enemy from Raycast
Hello Unity Community,
I am making a top-down shooter. I want to use Raycasting for the bullets of the guns. This is my first time using Raycasting to change an amount on another script so I am having some problems.
--Edit-- : This doesn't seem to work anymore and I did not change anything.... I got my script to change the amount of current health for one enemy, but when I duplicate that enemy and test to see if the health changed on those enemies after I shoot them, their currentHealth does not change.
--Edit-- It prints out that is has hit something so I think the problems is that the current health is not changing..
Am I doing something wrong? Please help me with this.
Here is the raycast part of my gun script:
void Fire()
{
currentBulletCount --;
Vector3 forward = muzzle.transform.TransformDirection(Vector3.forward);
Debug.DrawRay(muzzle.transform.position, forward * range, Color.green);
RaycastHit raycastBullet;
if(Physics.Raycast(muzzle.transform.position, forward, out raycastBullet, range))
{
if(raycastBullet.collider.tag == "Enemy")
{
enemyHealth = raycastBullet.collider.GetComponent<EnemyHealth>();//Gets the EnemyHealth script from the gameobject the ray hits
enemyHealth.AddjustCurrentHealth(-damage);//subtracts damge from the current enemy health
}
print("I hit " + raycastBullet.collider.gameObject.name);
//Destroy(raycastBullet.collider.gameObject);
}
}
And here is the Enemy Health Script:
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
//Public
public float currentHealth = 100f;
public float maxHealth = 100f;
public bool isDead;
void Update()
{
AddjustCurrentHealth(0);
}
public void AddjustCurrentHealth(float adj)
{
currentHealth += adj;
if(currentHealth <= 0)
{
currentHealth = 0;
isDead = true;
}
if(currentHealth > maxHealth)
{
currentHealth = maxHealth;
}
if(maxHealth < 1)
{
maxHealth = 1;
}
}
}
Any help is welcome
Thanks for helping me :D
Did you make sure the other enemies are tagged with "Enemy"? Everything seems fine in code so I don't think that's the problem.
Also, one thing I noticed is that your enemies have to reach a value lower than 0 to actually die ins$$anonymous$$d of dying when their health reaches 0. $$anonymous$$ight want to update your check to be currentHealth <= 0
.
Yes I did tag the enemy as "Enemy", that was the first thing I checked. And thanks for the notice on the below 0, I will change that right now
Answer by Infinite_Gamer · Oct 20, 2014 at 01:22 PM
Sorry for answering my own question.
I found out what I did wrong. I put two colliders on the game object because I put a collider on a empty game object then I put a sphere (and it has a collider too) inside of it. The raycast was detecting the sphere collider which was not tagged.
The above code works perfectly fine.
Answer by Thorny2000 · Oct 11, 2014 at 03:45 AM
The code above looks fine, assume you put a breakpoint inside AddjustCurrentHealth ti check it gets called? If not on "if(raycastBullet.collider.tag == "Enemy")" line and step inside there.
There is a Unity tutorial doing pretty much the same code here too, jump to 35:30: https://www.youtube.com/watch?v=Va77Gq8Cm8o
I have looked at the video and it did not help. I cant see any problems because when I shoot it prints out that is has hit something.. So I am guessing that problem has to be the EnemyHealth script changing the current health...
You mention printing again, do you not like breakpoints? Put breakpoints in all the functions from the hit detection to the health variable code. Step through the code. Watch the currentHealth value. Also check your public objects are setup and not null.
Hot tip: Also, you can set a breakpoint and only have it break if a variable changes: Set a breakpoint on a line (F9). Right click on the red breakpoint circle. Breakpoint properties. Tick "break when the expression changes"
I haven't really ever used breakpoints, but I will give this a try.
Answer by denisb · Oct 11, 2014 at 03:56 AM
There is a tutorial for that kind of game with everything you need to do it like you want http://unity3d.com/learn/tutorials/projects/survival-shooter
it is using raycast to hit the target obviously. you should go check it out.
Ok thanks for your fast responses. I will check out these videos. Thanks for all the help! :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[C#] Problem with RayCast check. 1 Answer
Raycasting, LineRenderer & Layermasks 0 Answers