Hi !
I try to achieve an bow system in Unity for HTC Vive.
I want to kill my enemy with only one arrow. But when my arrow hit the collider of my enemy nothing happen, i think i’m wrong with collider detection :/.
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == " Barbare" )
{
EnemyHealth enemyHealth = GetComponent <EnemyHealth> ();
if(enemyHealth != null)
Debug.Log(enemyHealth+"Enemy Health Avant");
{
enemyHealth.TakeDamage (damagePerShot );
Debug.Log(enemyHealth+"Enemy Health Apres");
}
}
else
{
ResetArrow();
}
}
This is for the arrow.
public void TakeDamage (int amount)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
if(currentHealth <= 0)
{
Death ();
}
}
For the Enemy damage.
( I use the VRTK package for the bow system )
Anyone ? Or if Someone know another for achieve something like that
is your enemy’s name exactly “Barbare” in the scene?
does either your arrow or enemy have a rigidbody?
is your arrow using trigger collision instead of normal collision?
I changed with this way :
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
EnemyHealth enemyHealth = GetComponent <EnemyHealth> ();
Debug.Log(enemyHealth+"Enemy Health Avant");
if(enemyHealth != null)
{
enemyHealth.TakeDamage (101);
Debug.Log(enemyHealth+"Enemy Health Apres");
}
}
In the debug log he display “Enemy Health Avant” so the arrow work with trigger solution. But he dont substract the health to my enemy. I changed the “damagePerShot” directly by the amount of damage but nothing change.
This is my EnemyHealth script :
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public float sinkSpeed = 2.5f;
public int scoreValue = 10;
public AudioClip deathClip;
Animator anim;
AudioSource enemyAudio;
ParticleSystem hitParticles;
CapsuleCollider capsuleCollider;
bool isDead;
bool isSinking;
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
if(isSinking)
{
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
//hitParticles.transform.position = hitPoint;
hitParticles.Play();
if(currentHealth <= 0)
{
Death ();
}
}
void Death ()
{
isDead = true;
capsuleCollider.isTrigger = true;
anim.SetTrigger ("Dead");
enemyAudio.clip = deathClip;
enemyAudio.Play ();
}
public void StartSinking ()
{
GetComponent <NavMeshAgent> ().enabled = false;
GetComponent <Rigidbody> ().isKinematic = true;
isSinking = true;
//ScoreManager.score += scoreValue;
Destroy (gameObject, 2f);
}
}
Are you sure the object you expect the arrow to collide with has a collider, and does it have the “EnemyHealth” script in it?
Yes i put my Enemy Health on my Enemy, and my arrow script on my arrow
my bad i didnt look at your script properly. in this line:
EnemyHealth enemyHealth = GetComponent <EnemyHealth> ();
by using GetComponent, you have tried to find “EnemyHealth” in the arrow.
You should be using this instead:
EnemyHealth enemyHealth = other.GetComponent <EnemyHealth> ();
Laperen:
my bad i didnt look at your script properly. in this line:
EnemyHealth enemyHealth = GetComponent <EnemyHealth> ();
by using GetComponent, you have tried to find “EnemyHealth” in the arrow.
You should be using this instead:
EnemyHealth enemyHealth = other.GetComponent <EnemyHealth> ();
Hops, i didn’t see that :S. Thanks a lot, sorry for the time !