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 Sayuka · Jun 27, 2014 at 11:58 AM · 2dtriggerdistancemelee

Melee for 2.5D Platformer? (C#)

I'm creating (as the title suggests) a 2.5D Platformer and wanted to add the option for the player to attack enemies in melee.

I am having some problems getting it to work though since I'm rather new to Unity.

My current attempt was to create a box collider that floats infront of my character and gets only set to "enabled" for the duration of one attack-swing. That part seems to work (from what I can tell by my Debug.Log entries. Though the hit is never regitered by the enemy.

My current code for the box-collider looks like this:

     public GameObject Player;
         bool attack = false;
     void Awake () 
     {
         Player = GameObject.FindGameObjectWithTag ("Player");
         gameObject.collider.enabled = false;
     }
 
     void Update () 
     {
         Player pstats = Player.GetComponent<Player>();
         attack = pstats.attacking;
 
         if (!attack) 
         {
             gameObject.collider.enabled = false;
         }
 
         if (attack) 
         {
             gameObject.collider.enabled = true;
             Debug.Log ("Box active");
         }
 
     }

The Enemyscript contains this:

     void OnTriggerEnter(Collider collision) 
     {
         Player pstats = Player.GetComponent<Player> ();
         if (collision.gameObject.tag == "Player") 
         {
             collision.gameObject.GetComponent<Player> ().PlayerHit ();
         }
         if (collision.gameObject.tag == "AttackBox") 
         {
             pstats.CurrentEXP += 25;
             EnemyDeath ();
         }
     }
 
     void OnTriggerStay(Collider collision) 
     {
 
         Player pstats = Player.GetComponent<Player> ();
         if (collision.gameObject.tag == "AttackBox") 
         {
             pstats.CurrentEXP += 25;
             EnemyDeath ();
         }
         
     }
 
     void OnTriggerExit(Collider collision)
     {
         Player pstats = Player.GetComponent<Player> ();
         if (collision.gameObject.tag == "AttackBox") 
         {
             pstats.CurrentEXP += 25;
             EnemyDeath ();
         }
     }

I tried different Trigger methods there, none seems to work.

A different idea I had was to drop the box collider and use the distance between player and enemy instead. Something like a test of sorts (pseudocode)

 if (distance player to enemy on x-axis < X-Range && distance on y-axis < Y-Range)
     Enemy gets hit

To me that even seems an easier solution, though I don't know how to implement it properly that I can messure the distance on one axis. Also I'd have to check if the player is infront or behind the enemy (I have two methods SlashRight and SlashLeft).

Any help would be highly appreciated, for either of the two approaches.

Comment
Add comment · Show 2
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 ABerlemont · Jun 27, 2014 at 01:50 PM 0
Share

If you don't need specific target area maybe you could work with the distance between your player and the enemy ?

float distance = Vector3.Distance(transform.position, enemy.transform.position);

for each frame the attack is on if the distance is lower than 1 or 2 units you are at melee distance and then just solve whatever you're trying to do ?

avatar image Sayuka · Jun 27, 2014 at 02:33 PM 0
Share

Well, that was the alternative idea I wrote in my question. Though I'd specifically need the distance on the X- and Y-Axis, Also, how do I get to my enemy.transform.position? don't I need to specify that as a gameobject "enemy"? If I do so, do I have to give each enemy a different tag or will something like "gameobject.findgameobjectwithtag("enemy")" just take the closest one? Or do I just do the calculation for each enemy at every run through update or the attack method?

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

change tiltshift distance with mousewheel 0 Answers

left and right 2d walk 1 Answer

2D Movement Problems 2 Answers

Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers

OnTriggerEnter2D using child's Trigger Box 1 Answer


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