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 CristiBala24 · Jul 27, 2020 at 06:38 PM · 2d gametopdownpixel artcombat

Help with 2D topdown combat

Hello! I have been working on a little top down game with action combat where if you press to the left of the character, the player attacks left, if he presses to the right, attacks right and so on for above and below. I have 4 hitboxes to the left, right, below and above which I activate and deactivate with the animations for attack. My 4 direction attack animations are set up in a blend tree so that might also contribute to my problem. The problem I have is that even tho I press to the left of the character, the above or below hitbox (depending if i press left-above or left-below) activates as well and so on for all my directions. No matter which direction I press 2 colliders will be enabled and not one. I will also attach my mouse direction script in case that is the problem and I will also provide photos of the hitboxes that are enabling. Thanks in advance!

 Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 mousePosition.z = transform.position.z;
 Vector3 attackDirection = (mousePosition - transform.position).normalized;
 
 Debug.Log(attackDirection);
 
  animator.SetFloat("MouseHorizontal", attackDirection.x);
  animator.SetFloat("MouseVertical", attackDirection.y);![alt text][1]


[1]: /storage/temp/164216-untitled-1.png

untitled-1.png (19.5 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jul 27, 2020 at 07:25 PM

Yes, the mouse clicking script that i sent earlier would cause various issues with this problem. This is because _when clicking to the left, your mouse click's y position may also detect that youre clicking above the player, causing two colliders to appear instead of the one you want. The solution that I would give is to add a deadzone. E.g:`public Camera cam; public GameObject player; public float Deadzone = 3f;

 void Update()
 {
     if (Input.GetMouseButtonDown(0)) //if i left click
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Vector3 Ray = ray.direction;
         RaycastHit hit; //raycasts need to hit solid objects to detect a hit

         if (Physics.Raycast(cam.transform.position, Ray, out hit)) //Shoot a raycast from my camera to where i clicked
         {
             if (hit.point.x < player.transform.position.x - Deadzone)
             {
                 Debug.Log("Left");
             }
             else if (hit.point.x >= player.transform.position.x + Deadzone)
             {
                 Debug.Log("Right");
             }

             if (hit.point.y < player.transform.position.y - Deadzone)
             {
                 Debug.Log("Down");
             }
             else if (hit.point.y >= player.transform.position.y + Deadzone)
             {
                 Debug.Log("Up");
             }
         }
     }
 }`

I found this to work well enough i guess, but if you want it to be perfect, you could create objects instead that would follow the player. When clicking on the objects, the click detected would never be both as you cant click on two at the same time. E.g:

     public Camera cam;
     public GameObject player;
     public float Deadzone = 3f;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0)) //if i left click
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Vector3 Ray = ray.direction;
             RaycastHit hit; //raycasts need to hit solid objects to detect a hit
 
             if (Physics.Raycast(cam.transform.position, Ray, out hit)) //Shoot a raycast from my camera to where i clicked
             {
                 if (hit.collider.gameObject.CompareTag("Left"))
                 {
                     Debug.Log("Left");
                 }
                 else if (hit.collider.gameObject.CompareTag("Right"))
                 {
                     Debug.Log("Right");
                 }
                 else if (hit.collider.gameObject.CompareTag("Up"))
                 {
                     Debug.Log("Up");
                 }
                 else if (hit.collider.gameObject.CompareTag("Down"))
                 {
                     Debug.Log("Down");
                 }
             }
         }
     }

And the setup ingame would look something like this for 3D: alt text


weirdsetup.png (247.1 kB)
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

173 People are following this question.

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

Related Questions

2D topdown combat collider vs OverlapBox etc... 1 Answer

[2D] Enable colliders around the player based on the direction of the mouse click 1 Answer

When should I use tilemap and game object? 1 Answer

How to animate topdown 2d water tiles? 1 Answer

How do I make a 2d bullet object move towards the players original position when it is spawned? 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