• 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 t$$anonymous$$s 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

t$$anonymous$$s 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, w$$anonymous$$le 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) instead 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
6
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 t$$anonymous$$s:

 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 $$anonymous$$t : RaycastHit;
 
     if(Physics.Raycast(ray,$$anonymous$$t,500))
     {
     //drawray only to visualize
     Debug.DrawRay(Camera.main.transform.position,ray.direction*$$anonymous$$t.distance,Color.green);
     if($$anonymous$$t.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 Mesh 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Translate Point By Vector3 And Angle 1 Answer

How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers

Question about sending physics raycast from the camera 1 Answer

How convert the coorardinates of the mouse from one camera to another? 2 Answers

Camera/mouse movement issues "almost but no dice" 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