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 Madmogga · Sep 26, 2012 at 07:06 PM · health

Help with dammage script

Hi I am quite new to Unity and i have been following a few video tutorials to help me learn. I have created a basic level and have two sentry guns which work and fire and cause damage to my first person controller (all good so far)

The problem I have is causing damage and destroying my sentry guns, I have use the same type of scripts like the player health, but something strange happens, both the sentry guns explode as soon as I shoot regardless of the damage or if the bullets hit.

Can anyone post a simple script one for my projectile to cause damage and one for the sentry gun to receive it I would be grateful.

Comment
Add comment · Show 1
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 MD_Reptile · Sep 26, 2012 at 07:15 PM 2
Share

well, you should really post the part of your code that handles the damage, so we can see if it just needs a little work, but I cant help you if I dont know what your working with now.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CaioRosisca · Sep 26, 2012 at 08:15 PM

You can try this:

to apply damage to an object you can do the following:

when your bullet collides with the object that will receive the damage do this:

apply this script to your bullet.

 void OnCollisionEnter(Collision col)
 {
     col.gameObject.SendMessage ("ApplyDamage", 5.0, SendMessageOptions.DontRequireReceiver);
 }

gameObject must be the gameObject that will receive the damage(you can assign it or use the parameter received with your colliding function)..

then on the damage receiver object create this function:

 void ApplyDamage (Float damage) {
     currentHeal -= damage;
     print (damage);
 }

this will remove X hitPoints from your current health.

Comment
Add comment · 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
0

Answer by Madmogga · Sep 26, 2012 at 09:12 PM

Sorry was at work at the time, my dammage script is as follows

var rayCastLength = 5;

function Update () { var hit : RaycastHit;

if (Physics.Raycast (transform.position, transform.forward, hit, rayCastLength)) { if(hit.collider.gameObject.tag == "Mini");

Healthminigun.GUNHEALTH -=50; Destroy (gameObject,.2); }

}

Comment
Add comment · 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
0

Answer by Madmogga · Sep 26, 2012 at 09:12 PM

sorry i guess it would help if i posted my script, below is what i have on my projectile

var rayCastLength = 5;

function Update () { var hit : RaycastHit;

if (Physics.Raycast (transform.position, transform.forward, hit, rayCastLength)) { if(hit.collider.gameObject.tag == "Mini");

Healthminigun.GUNHEALTH -=50; Destroy (gameObject,.2); }

}

and this is what is on my sentri gun

var explosion : GameObject; static var GUNHEALTH = 100;

function Update () {

if(GUNHEALTH <=0) { var explosionClone = Instantiate(explosion,transform.position, transform.rotation); explosionClone.GetComponent("Detonator"); Destroy (gameObject,.6); Destroy (rigidbody); }

}

i am copying snipts of code trying to understand what each does so any help please

thanks

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 shaderop · Sep 26, 2012 at 09:21 PM 0
Share

You should edit your original question with the additional information ins$$anonymous$$d of adding new answers that are not really answers. I know it can be tempting to use this website like a forum, but it really isn't meant to be used that way.

avatar image CaioRosisca · Sep 26, 2012 at 10:43 PM 0
Share

O$$anonymous$$.. when you calculate your Raycast inside Update() this means it will be checking for that hit everyframe, so if you deal 50 damage to your turret in case the raycast it hitting it, within 2 frames you will have dealt 100 damage(turret´s total health). That´s why it gets destroyed so fast, so ins$$anonymous$$d of using a raycast to calculate if you have hit your target try to use a collision detection like I´ve said on my answer. For this you just need a collider on your projectile. If you do this and use the code I have posted on my answer your turret will detect it has been hit just once.

To make sure that you are apllying damage too often, after this line of your code:

Health$$anonymous$$igun.GUNHEALTH -=50; Destroy (gameObject,.2);

put this:

print("Damage applied");

and watch your console window to see how amny times if happens.

also, if using my solution after:

"col.gameObject.Send$$anonymous$$essage ("ApplyDamage", 5.0, Send$$anonymous$$essageOptions.DontRequireReceiver);"

destroy your projectile using:

Destroy (gameObject)

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make Enemy AI and Health 1 Answer

Enemy Health Damage 2 Answers

How can I lose health when my enemy collides with the player? 1 Answer

House/building losing health 3 Answers

Shooting and health Help 0 Answers


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