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 Dreave · Apr 14, 2012 at 01:38 PM · destroyenemydamage

Destroy enemy using raycasts

Hi all, this script is attached to an invisible cube at the end of my gun it fires raycasts and if it hits anything in the scene it spawns a particle effect of sparks. If the rays collide with something with a tag of "Enemy" then it spawns a blood particle effect, this all works how it should apart from the ApplyDamage part which is supposed to destroy the enemy if its health goes to zero. This dosent work as my Enemy dosent have health and dosent know when to die, this is what I think anyway. Is my Enemy supposed to have a damage receiver sort of script which tells it when to destroy itself, I need some help with this can anyone lend a hand?

script

 var amountOfShots = 30;

var reloadTime = 1.5;

var refireRate : float = 0.1;

function ShotPoller()

{

while(true)

{

 if(Input.GetButton("Fire1"))

 {

     Shoot();

     

     yield WaitForSeconds(refireRate);

 }

 if(Input.GetKeyDown("r")){

     Reload();

 }



 yield;

}

}

function Start()

{

StartCoroutine(ShotPoller());

}

if(Input.GetKeyDown("r")){

Reload();

}

function Reload (){

yield WaitForSeconds(reloadTime);

amountOfShots = 30;

}

var shotSound : AudioClip;

var bloodPrefab : Transform;

var sparksPrefab : Transform;

var hit : RaycastHit;

var range = 500;

function Shoot (){

 if(amountOfShots > 0){

     amountOfShots--;

     if(shotSound){

     audio.PlayOneShot(shotSound);

 }



if (shotSound) audio.PlayOneShot(shotSound);

var hit: RaycastHit;

if (Physics.Raycast(transform.position, transform.forward, hit)){

 var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);



 if (hit.transform.tag == "Enemy"){ 



     if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); 



     hit.transform.SendMessage("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver); 



 } else { 



     if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);



 }



}

}

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Piflik · Apr 14, 2012 at 05:07 PM

I would simply add a public variable to the enemy called "health" and replace the "SendMessage" line at the end of your script with:

 hit.GetComponent(EnemyScript).health -= 20;

The enemy would now have to test if his health is smaller than 0 and if so it would destroy itself.

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 bodec · Apr 14, 2012 at 01:46 PM

yes towards the bottom of your script where it says apply damage is your damage to your enemy that should be tied into a function on this script or another script that accesing the enemys 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

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

6 People are following this question.

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

Related Questions

Destroy Scripting Help 1 Answer

Enemy damage error 1 Answer

Enemy's wont die if spawned 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy Follow Script Help 2 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