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 04:57 PM · 2dmousepositiontopdownpixel artroguelike

How to check if mouse is above or below player?

Hello there! I have been working on a top down 2D roguelike with action combat. The player attacks in the direction of the mouse click. I know that if you want to check if the mouse is to the left or to the right of the player you have to do something like if(mousePosition.x > player.transform.position.x) and the other way around for the right. Now the problem i am having is how do i detect if the mouse was pressed above or below the player and how do i check in an if chain if the mouse was pressed to the right, left, up or down? Thanks in advance!

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
Best Answer

Answer by Llama_w_2Ls · Jul 27, 2020 at 05:11 PM

     public Camera cam;
     public GameObject player;
 
     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)
                 {
                     //I clicked on the left side of the player
                 }
                 else if (hit.point.x >= player.transform.position.x)
                 {
                     //I clicked on the right side of the player
                 }
             }
         }
     }

I dont know if raycasts work for 2D but here goes anyway

Comment
Add comment · Show 4 · 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 CristiBala24 · Jul 27, 2020 at 05:23 PM 0
Share

@Llama_w_2Ls hey thanks for the answer. The problem is that i need to check if it is above or below not left or right. If you could help me with that by integrating below and above in the code you wrote i would really appreciate it. Have a nice day!

avatar image Llama_w_2Ls CristiBala24 · Jul 27, 2020 at 05:42 PM 0
Share
     public Camera cam;
     public GameObject player;
 
     void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(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.y < player.transform.position.y)
                 {
                     //I clicked above the player
                 }
                 else if (hit.point.y >= player.transform.position.y)
                 {
                     //I clicked below the player
                 }
             }
         }
     }
avatar image CristiBala24 Llama_w_2Ls · Jul 27, 2020 at 05:45 PM 0
Share

@Llama_w_2Ls Thanks it worked! How would i go about combining the two scripts you wrote so i can have it go around the player: left, right, above and down?

Show more comments
avatar image
0

Answer by SeDevr · Jul 27, 2020 at 07:11 PM

i dont know exactly, but i think you can find some data on this if you search for the keycodes online( i dont know if that helps)

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

284 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 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

Unity2D Pixels deforming when moving 0 Answers

Horrible lag in 2d roguelike character movement 6 Answers

How do I make my character move on the Z axis on 2d rigidbody? 0 Answers

How in the world do I ACCURATELY place an object at the mouse? 3 Answers

[2D] Enable colliders around the player based on the direction of the mouse click 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