- Home /
How To Check If A Bullet Hits Another Object (player) From Another Script
In my script I have a bullet initiate whenever the enemy gets close, but I need to change the PlayerHealth variable so it can't be a script on the bullet GameObject. Because of this I need to find a way to
Access the bullet (which should be easy)
check if it collides with the player
and if it does take off 10 health
Here is my script:
#pragma strict
var Player : Transform;
public var MoveSpeed = 10;
public var MaxDist = 7;
public var MinDist = 5;
public var AttackDistance = 5;
public var PlayerHealth = 300;
public var EnemyHealth = 10;
var ani : Animator;
var AnimAttack : boolean;
var AnimDie : boolean;
var AnimShoot : boolean;
public var IsDead : boolean;
var bullet : GameObject;
var spawnPoint : Transform;
function Start ()
{
ani.enabled = false;
AnimAttack = false;
AnimDie = false;
IsDead = false;
AnimShoot = false;
}
function Update ()
{
if(IsDead) {
Die();
}
else {
if(gameObject.tag == "EnemyMelee") {
if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
transform.LookAt(Player);
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= AttackDistance)
{
if(Vector3.Distance(transform.position,Player.position) <= AttackDistance && Input.GetMouseButtonDown(0)) {
EnemyHealth = EnemyHealth - 1;
print(EnemyHealth + " = Enemy Health");
}
else {
AttackMelee();
}
}
if(EnemyHealth <= 0) {
IsDead = true;
}
if(PlayerHealth <= 0) {
Application.LoadLevel(1);
}
if (AnimAttack == true) {
ani.SetBool("AnimAttack", true);
ani.SetBool("AnimDie", false);
}
if (AnimDie == true) {
ani.SetBool("AnimAttack", false);
ani.SetBool("AnimDie", true);
}
}
else {
}
}
if(gameObject.tag == "EnemyRanged") {
if(IsDead) {
Die();
}
else {
if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
transform.LookAt(Player);
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= AttackDistance)
{
if(Vector3.Distance(transform.position,Player.position) <= AttackDistance && Input.GetMouseButtonDown(0)) {
EnemyHealth = EnemyHealth - 1;
print(EnemyHealth + "= Enemy Health");
}
else {
AttackRanged();
}
}
if(EnemyHealth <= 0) {
IsDead = true;
}
if (AnimShoot == true) {
ani.SetBool("AnimAttack", false);
ani.SetBool("AnimDie", false);
ani.SetBool("AnimShoot", true);
}
if (AnimDie == true) {
ani.SetBool("AnimAttack", false);
ani.SetBool("AnimShoot", false);
ani.SetBool("AnimDie", true);
}
}
else {
}
}
}
}
}
function Die() {
ani.enabled = true;
AnimAttack = false;
AnimDie = true;
yield WaitForSeconds (3);
Destroy (gameObject);
}
function AttackMelee() {
PlayerHealth = PlayerHealth - 0.000001;
print(PlayerHealth);
ani.enabled = true;
AnimAttack = true;
AnimDie = false;
}
function AttackRanged() {
ShootGun();
print(PlayerHealth);
ani.enabled = true;
AnimAttack = false;
AnimDie = false;
AnimShoot = true;
}
function ShootGun (){
var nBullet = Instantiate(bullet, spawnPoint.position, spawnPoint.rotation);
nBullet.GetComponent.<Rigidbody>().velocity = spawnPoint.TransformDirection(Vector3(0,0,100));
//Check collision here!!!
}
Answer by · Feb 19, 2017 at 11:04 PM
I fixed it using the same method I used on every other object to detect whether the enemy the script is attached to: I checked the object's tag. I'll check the tag of the object the script is attached to, if it's tag is 'Bullet' remove 10 from the variable PlayerHealth!
Your answer
Follow this Question
Related Questions
Collision detection for an initialized prefab 0 Answers
Initialising List array for use in a custom Editor 1 Answer
OnDestroy collision detect 4 Answers