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 D3m0nE · Feb 15, 2014 at 11:36 AM · cameraraycastvector3mouse

Detect Mouse in right side or left side For Player?

Hello, i use this code to detect Moues Position

  mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
  
 if(mouse.x < Screen.width / 2)
 {
 print("Mouse is on left side of screen.");
 }
  
 if(mouse.x > Screen.width / 2)
 {
 print("Mouse is on right side of screen.");
 }


but there is 1 problem

this code is detect the mouse if right side or left side for the Camera

but i need to detect the mouse if right side or left side for the Player,

because player can move, while camera is static [not moving with player]

alt text

eerrr.jpg (18.1 kB)
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 Linus · Feb 15, 2014 at 12:00 PM 0
Share

Tried doing the same but with player.transform.position.x (or whatever your player object is named) ins$$anonymous$$d of Screen.width / 2?

avatar image D3m0nE · Feb 15, 2014 at 01:18 PM 0
Share

yes i tried same problem

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by whydoidoit · Feb 15, 2014 at 01:44 PM

You want to use Camera.WorldToScreenPoint to convert the coordinate of the player to the screen and then work out if the mouse is left or right of the player.

    var playerScreenPoint = Camera.main.WorldToScreenPoint(player.transform.position);

     if(mouse.x < playerScreenPoint.x) {
         ... 
     } else {
        ....
     }
Comment
Add comment · Show 2 · 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 D3m0nE · Feb 15, 2014 at 01:48 PM 0
Share

Thank you :) thats working fine

avatar image whydoidoit · Feb 15, 2014 at 01:49 PM 0
Share

No problem :)

avatar image
1

Answer by erick_weil · Feb 15, 2014 at 12:04 PM

place a plane as background to your scene, and change the name of player to player ant try out this:

 mouse = Vector2(Input.mousePosition.x,Input.mousePosition.y);
 var player : Vector3 = GameObject.Find("player").transform.position;
 var ray : Ray;
 ray = Camera.main.ScreenPointToRay(mouse);
 var hit : RaycastHit;
 
     if(Physics.Raycast(ray,hit,500))
     {
     //drawray only to visualize
     Debug.DrawRay(Camera.main.transform.position,ray.direction*hit.distance,Color.green);
     if(hit.point.x < player.x)
     print("Mouse is on left side of player.");
     else
     print("Mouse is on right side of player.");
     }
Comment
Add comment · Show 3 · 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 D3m0nE · Feb 15, 2014 at 01:15 PM 0
Share

works fine . thanks but is there way without using plane as background?

avatar image erick_weil · Feb 15, 2014 at 01:36 PM 1
Share

why?, any collider will work, and if you disable the $$anonymous$$esh renderer in the plane you will get same results.

if have problems by the other objects in the front, if your camera is ortographic, just put the plane very very near from the camera. but if is not, I dont know how you solve this

avatar image D3m0nE · Feb 15, 2014 at 01:47 PM 0
Share

okey. :) thanks for help

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

21 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

Related Questions

Raycast Object Selection 3 Answers

Ray through camera 1 Answer

2d Position to 3d Position 2 Answers

Unity Android Movement Problem 0 Answers

Ray camera to Terrain goes Wrong. 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