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
1
Question by Dreave · Oct 10, 2011 at 03:41 PM · ai

Enemy Damage

I know this is a duplicated question but the other kinda went dead. Ive been following the berg zerg arcade hack and slash tutorial to help me with my enemy's, but because he's making a hack and slash game his way of killing the enemy is different to mine. In the script below it says to take damage of when close enough to the enemy and if facing the right direction and so on whereas I want it to take damage of my enemy when my bullet prefab has contact with it. Pleas can anyone help me?

 using UnityEngine;

using System.Collections;

public class PlayerAttack : MonoBehaviour { public GameObject target; public float attackTimer; public float coolDown;

 // Use this for initialization
 void Start () {
     attackTimer = 0;
     coolDown = 0.30f;
 }
 
 // Update is called once per frame
 void Update () {
     if(attackTimer > 0)
         attackTimer -= Time.deltaTime;
     
     if(attackTimer < 0)
         attackTimer = 0;
     
     if(Input.GetButtonDown("Fire1")) {
         if(attackTimer == 0) {
             Attack();
             attackTimer = coolDown;
         }
     }
 
 }
 
 private void Attack() {
     float distance = Vector3.Distance(target.transform.position, transform.position);
     
     Vector3 dir = (target.transform.position - transform.position).normalized;
     
     float direction = Vector3.Dot(dir, transform.forward);
     
     Debug.Log(direction);
     
     if(distance < 2.5f) {
         if(direction > 0) {
     EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
     eh.AddjustCurrentHealth(-10);
         }
     }
 }

}

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
1

Answer by cmccown · Oct 10, 2011 at 05:58 PM

Have you tried making your bullet prefab a rigidbody and making your enemies triggers?

You can create a OnTriggerEnter event handler in the bullet prefab, assign your enemies a tag like ,"HostileTarget" or whatever.

Then in the OnTriggerEnter event handler test the Collider's parameter's tag property to determine if it has the tag you're using to identify enemies.

You can pass damage up to an enemy by sending the Collider a message containing the damage using SendMessageUpwards to pass the function and damage parameters to your enemy class.

Comment
Add comment · Show 6 · 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 cmccown · Oct 10, 2011 at 06:46 PM 0
Share

The script for this would be something along the lines of (keep in $$anonymous$$d, I'm at work with no intellisense, so research this in the docs): function OnTriggerEnter(other : Collider) { if(other.tag == "HostileTarget") { other.Send$$anonymous$$essageUpwards("ApplyDamage"), damageAmount); } }


You'll need a function on your Enemy base class that all enemy types inherit from that looks something like this: ApplyDamage(damage : int){ health -= damage;}


You'll also need a property in your bullet prefab to represent the damage that projectile causes.

Depending on the type of weapon, you might want to follow ApollyonBob's advice below and use raycasting.

"Bullets" or "Lazahs" fly too fast to bother instantiating, but for something like a missile, grenade, slingshot type weapon that moves slow or follows a trajectory that needs to be modified in flight you'll want to create an actual object instance to represent.

avatar image Dreave · Oct 10, 2011 at 08:32 PM 0
Share

There is an error saying unexpected token: ,. Ive tried to fix it but I'm not the best of scripters, could you please help?

avatar image cmccown · Oct 11, 2011 at 12:44 AM 0
Share

Below is an event handler I've used before for missiles striking an enemy object. You'll have to edit the script, but it will be good practice.

function OnTriggerEnter(other : Collider) { if(other.gameObject.tag == "HostileTarget") { other.gameObject.Send$$anonymous$$essageUpwards("ApplyDamage", 5);
Instantiate(explosion, transform.position, transform.rotation); Destroy(this.gameObject); } }

As stated in the previous comment you'll need to implement a function in your enemy objects that handles applying the damage. If you use this script it must be called ApplyDamage and it must take an integer input. I'm still not convinced you should be using this method. If you're looking for a beam or bullet type weapon you should definitely try using a Raycast ins$$anonymous$$d.

avatar image Dreave · Oct 11, 2011 at 03:40 PM 0
Share

So do I add the script in your first comment onto my bullet prefab and the script in your second comment to my enemy?

avatar image cmccown · Oct 11, 2011 at 03:44 PM 0
Share

The event OnTriggerEnter would go in your bullet prefab.
You'll need another script called ApplyDamage that exists in your enemies. Each enemy needs to be market with the tag "HostileTarget".

The amount of damage caused by your weapon would be modified by changing the value passed into ApplyDamage. (currently 5)

Show more comments
avatar image
0

Answer by apollyonbob · Oct 10, 2011 at 06:35 PM

To be honest, I don't think a bullet prefab is the best way to go. Especially since if the player can see your bullets, they are moving way too slow.

The best way to detect whether or not someone has shot the enemy is to do a Physics.RayCast and use the collider information to see what was hit. You basically shoot the ray out of the front of your gun, and it'll tell you if you hit an enemy, hit a wall, etc. Then whatever you hit should have a hit reaction - a blood spurt on an enemy, a spark on a wall (not realistic, but hollywood has condition people to expect it, haha) - combined with the flash from the muzzle, it'll look like a gunshot.

You can modify the ray for each shot to do things like bullet drift, etc. (It won't actually arc, but you're moving the end point around. Easy way to cheat.)

Physics.RayCastAll will tell you everything the ray hit, instead of just the first thing. (This might be useful if you want your bullets to penetrate the enemy then go beyond him.)

I realize that this doesn't answer your immediate question, but I think this is the best way to accomplish your goal.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a moving gameobject approach and stop at a target (arrival behaviour) 0 Answers

A node in a childnode? 1 Answer

AI using Javascript 1 Answer

Clearing a destination... 1 Answer

Stay Back! 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