Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 track01 · Aug 27, 2011 at 10:13 AM · ontriggerentermeleecombat

OnTriggerEnter - Melee Combat

Can someone explain how this is done please? I'm trying to do melee combat for my game and I have animated my sword to swing. I've never used this before and have no idea how to implement it and the api isn't very helpful.

I have added an empty object and a box collider around the blade of the sword. I've added it as a child of the sword and have ticked Is Trigger. This is the script I have so far.

 function OnTriggerEnter ( other : Collider )
 
 {
 
     if ( other.tag == "Enemy" ) // or what ever you want to identify the object
 
     {
 
         other.GetComponent("EnemyHealth1").AdjustCurrentHealth(-10);
 
         Debug.Log("Enemy Hit!!");
 
     }
 
 }

But it doesn't seem to be doing anything.

This is the script for the health and AdjustCurrentHealth

 var hp:float; // health value, 0..maxHp
 
 var maxHp:float; 
 
 var healthBarWidth:int = 20;
 
 var myHealthBar:GameObject;
 
 private var myHb:GameObject;
 
 var posX:float;
 
 var posY:float;
 
 var show: float = 0;       // health bar appears when show > 0
 
 var duration: float = 0.6; // health bar vanishes for duration seconds
 
 
 
 function Start () {
 
   myHb = Instantiate(myHealthBar, transform.position, transform.rotation);
 
 
 
 }
 
 
 
 function AdjustCurrentHealth(adj : int) {
 
    hp += adj;
 
 }
 
 
 
 function Update () {
 
   if (show>0){
 
     myHb.guiTexture.color.a = show; // variable show controls the bar alpha
 
     show -= Time.deltaTime/duration; // fades out health bar unless show is recharged
 
     myHb.transform.position = Camera.main.WorldToViewportPoint(transform.position);
 
     myHb.transform.position.x -= posX;
 
     myHb.transform.position.y -= posY;
 
   //  myHb.transform.localScale = Vector3.zero; // <- this does nothing
 
     var healthPercent:float = Mathf.Clamp(hp / maxHp, 0, 1);
 
     myHb.guiTexture.pixelInset = Rect (0.02, -0.09, healthBarWidth * healthPercent, 5);
 
   }
 
 }

The health script is attached to my enemy and the melee attached the the empty game object with the box collider.

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 Owen-Reynolds · Aug 28, 2011 at 02:09 AM 0
Share

Try starting with a smaller test: comment out AdjustCurrentHealth and try to get only the Debug to run. Things to check: has the enemy tag been set to "Enemy"? Try selecting the arm in sceneView to be sure it's moving with you. In general, the "moving triggerBox" idea does work.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Peter 8 · Jun 28, 2012 at 04:48 PM

OnTrigger functions require a rigid body attached as stated here: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html. usually put it on the moving element. just set the rigid body to is kinematic so it isnt affected by any forces acted upon it.

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

How can i detect collision in melee combat? 2 Answers

Multiple Melee Weapons 2 Answers

Melee Combat 1 Answer

melee combat 0 Answers

Trigger and ennemy damage 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