Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lindsay4410 · Feb 18, 2015 at 10:17 AM · scripting problemcollisionenemydamage

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lindsay4410 · Feb 18, 2015 at 08:42 PM 0
Share

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);


???

avatar image Firedan1176 · Feb 18, 2015 at 08:46 PM 0
Share

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:

// Set the position of the particle system to where the hit was sustained.

hitParticles.transform.position = hitPoint;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges