- Home /
Damage on prefab collision?
Hi everyone. I'm on my second day scripting and I'm pretty lost. I was following a tutorial here
but I'm trying to alter it to make the game mine. Instead of having a raycast laser shoot, I was having my guy toss rocks. I have a character that moves, and throws the rocks, and the rocks are colliding with my baddies. The baddies aren't taking damage and I'm not sure how to alter the script so that they do.
Here's what I have under my "enemyHealth" script:
public void TakeDamage (int amount, Vector3 hitPoint) { // If the enemy is dead... if(isDead) // ... no need to take damage so exit the function. return;
// Play the hurt sound effect.
enemyAudio.Play ();
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
// Set the position of the particle system to where the hit was sustained.
hitParticles.transform.position = hitPoint;
// And play the particles.
hitParticles.Play();
// If the current health is less than or equal to zero...
if(currentHealth <= 0)
{
// ... the enemy is dead.
Death ();
}
}
Here's what I have under my PlayerShooting script:
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets)
{
// ... shoot the gun.
Shoot ();
}
}
void Shoot ()
{
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(-8,0,speed));
}
}
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Dinosaur1")
{
// Try and find an EnemyHealth script on the gameobject hit.
EnemyHealth enemyHealth = collider.GetComponent <EnemyHealth> ();
// If the EnemyHealth component exist...
if(enemyHealth != null)
{
// ... the enemy should take damage.
enemyHealth.TakeDamage (damagePerShot);
}
}
}
}
I'm getting the error "Assets/Scripts/PlayerShooting.cs(62,45): error CS1501: No overload for method TakeDamage' takes
1' arguments"
I get the feeling that the script isn't properly defining "TakeDamage" I know what I want to say with the script but I don't know how to say it.
I want it to say "When ThrowRock(clone) interacts with Enemy collision, have enemy take health damage equal to damagePerShot. If Enemy health is equal to 0, Enemy Dies."
Any help anyone can offer would be GREATLY appreciated! Again, this is my second day scripting. I did a lot of research, but everything seems to not apply to my situation. I feel like I've gotten pretty tangled and I'm not sure how to get it straightened out. Thanks!
Answer by _MGB_ · Feb 18, 2015 at 10:24 AM
That error is telling you that the 'TakeDamage' function takes more than one argument. Looking at the fn it does indeed take two args: amount and position.
You just need to pass in the hit position as well as the damage. Looking at your code you could get this from the Collision parameter. See http://docs.unity3d.com/ScriptReference/Collision.html
Hi there. Thank you for answering. I'm not sure I understand your answer... please remember it's day 3.
I see what you're saying about it needing both an amount (damagePerShot) and a position (the collision, right?)
Not sure I understand what the syntax should be.
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Dinosaur1")
{
// Try and find an EnemyHealth script on the gameobject hit.
EnemyHealth enemyHealth = collider.GetComponent <EnemyHealth> ();
// If the EnemyHealth component exist...
if(enemyHealth != null)
{
// ... the enemy should take damage.
enemyHealth.TakeDamage (damagePerShot, transform.position);
???
That's the idea, but you don't want to use transform.position
. You want to use where the collision was, so something like this:
enemyHealth.TakeDamage (damagePerShot, col.transform.position);
On your EnemyHealth script you posted above, look at line 7 and 8:
hitParticles.transform.position = hitPoint;// Set the position of the particle system to where the hit was sustained.
Your answer
Follow this Question
Related Questions
Damage on collision with player.. 1 Answer
Collision Damage 1 Answer
Enemy deals damage to player on contact 2 Answers
How i make the player damage the enemy specific enemy he is colliding with 1 Answer
Having problems with damage script 1 Answer