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 Aydan · Apr 24, 2011 at 10:45 AM · playerenemyattack

Attacking enemys

Hi I'm looking for a basic player attack script that works like oblivion's combat system I've tried burgzergarcade but I'm not trying to do the targeting thing I want to be able to walk up to a random enemy and attack them and they loose health.

heres my PlayerAttack script:

var target : GameObject; var attackTimer : float; var coolDown : float; var damage : float = 10;

// Use this for initialization function Start () { attackTimer = 0; coolDown = 1.0f;

}

// Update is called once per frame function Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;

 if (attackTimer < 0)
     attackTimer = 0;
 if(Input.GetKeyUp(KeyCode.F)) {
     if(attackTimer == 0){
         Attack();
         attackTimer = coolDown;
     }
 }

} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);

 var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);

 Debug.Log(direction);

 if(distance < 3) {
     if(direction > 0) {
         target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
     }
 }

}

how can I get target to be the enemy infront of me and change when I go to another enemy?

and heres my EnemyHealth script:

var curHealth : float = 20; var maxHealth : float = 20;

function Update () { AddjustCurrentHealth(0);

}

function AddjustCurrentHealth(adj) { curHealth += adj;

 if(curHealth < 0)
     curHealth = 0;

 if (curHealth > maxHealth)
     curHealth = maxHealth;

 if(maxHealth < 1)
     maxHealth = 1;

 if(curHealth < 1) { //die
     Destroy(gameObject);
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDemiurge · Apr 24, 2011 at 11:53 AM

Haven't played Oblivion so I can't tell you how to do that, specifically. But I can give you a nice easy way to do damage in general :)

A fairly simple way to do it without going overboard is the SendMessage and BroadcastMessage functions. What they do is tell another GameObject to try to run the function you name, on every MonoBehaviour that's attached to it. The only difference with Send and Broadcast is that Broadcast also takes the "message" to all the children of the object.

So for example you decide in advance that every character has a TakeDamage function that takes 1 parameter - the damage number as a float. So, if your target is a GameObject, you'd say something like target.SendMessage("TakeDamage", 5.0); with 5.0 being how much damage to apply. Look up the documentation for these functions because there's an optional third parameter that can be handy if you're not 100% positive your target even has the TakeDamage function.
But it doesn't have to be a float, it can be anything:

enum DamageType { Piercing, Blunt, Slashing, Magic }

struct Damage { var damage: float; var type: DamageType; var armorPenetration: float; }

. . . var dmg: Damage; target.SendMessage("TakeDamage", dmg);

Your TakeDamage function just needs to accept a parameter of type Damage in this case, and it's up to do what you need based on that information.

Comment
Add comment · Show 1 · 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 Aydan · Apr 24, 2011 at 11:21 PM 0
Share

I'm looking for something that kinda targets the enemy's in front of me automatically but thanks

avatar image
0

Answer by zmar0519 · Apr 24, 2011 at 12:46 PM

What TheDemiurge said is correct. Also, look here for a key combo script, which may be what your looking for.

Comment
Add comment · Show 1 · 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 Aydan · Apr 24, 2011 at 11:19 PM 0
Share

no its not what I'm looking for but thanks, I'm wanting sort of an auto targeting system kinda. But thanks anyway

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

No one has followed this question yet.

Related Questions

How to make my player not lose health when attacking the enemy? 2 Answers

auto select enemys 3 Answers

About trigger 3 Answers

How to stop enemy within certain distance of player? 2 Answers

Problems with enemy attack script! 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