Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ninjathaum · Feb 09, 2016 at 05:08 PM · cameraanglemousepositionscreentoworldpointboundary

Input.mousePosition vector3 in a Camera.ScreenToWorldPoint method not working properly when camera hits its boundary, unity2D

     transform.parent.transform.position = thePlayer.position;
     mouse = Input.mousePosition;
     mouse = new Vector3(mouse.x, mouse.y, 1);
     mouse = Camera.main.ScreenToWorldPoint(mouse);
     Vector3 dir = mouse - thePlayer.position; 
     float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; 
     print("" + mouse + "___" + thePlayer.position + "___" + dir);
     transform.parent.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);  

now this works well and good when my camera has not stopped at its bounds, but when the camera does stop and the player moves to the bounds and i don't move my mouse cursor, the mouse vector keeps going up even when i am not moving the mouse, this then causes the 'mouse - thePlayer.position' Vector3 to be wrong and generate the wrong angle.

Iv'e been trying to figure it out and just can't come to a decent solution! The only things i could think of are the mouse pos to world pos is thinking the camera should have moved so much over the boundary and moves the still mouse that way in terms of world position even though it's stationary in terms of screen and world space.

In terms of boundaries, the camera simply cannot go past x / y -50 -> x / y +50, so i'd expect the maximum/minimum mouse position in world position terms would be 50 or -50 but it goes up to about +- 63, these extra values are when the player is moving when the camera has stopped at the boundary and mouse pos keeps increasing. The image below shows the positions of the mouse, player and the dir Vector3 as stated in the print() in my code. this is when the mouse is on the left side of the player, but the distances are far greater than they should be.

So yeah, thanks!

[1]: /storage/temp/63456-positions.png

positions.png (2.4 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 NoseKills · Feb 09, 2016 at 06:24 PM 0
Share

when the camera does stop and the player moves to the bounds and i don't move my mouse cursor

I'm not sure that i understand. If 'mouse' is the cursor world position and thePlayer.position; is the player world position, if either of them change in relation to the other, then 'Vector3 dir' and 'float angle' will change too.

According to your explanation 'mouse' should be constant but the player moves so sounds like the changing of the angle is expected.

What is the expected result and what did I misunderstand? :)

avatar image Ninjathaum NoseKills · Feb 13, 2016 at 02:48 PM 0
Share

One thing i probably should have mentioned was the player is in the middle of the camera and the camera follows the player, but when the camera hits its boundaries, the player can move freely to that boundary without the camera following.

Basically i am setting boundaries for the camera and when the camera reaches that point, the camera won't move in that direction anymore, When the camera hasn't hit these boundaries the Vector 3 that the Input.$$anonymous$$ousePosition method gives would be the same as my players position if i put my mouse directly on top of the player, the problem is when the camera has say hit the left boundary and the player continually moves left until he hits the edge, if i were to then put the mouse directly on top of the player, the 2 positions in world space do not equal the same thing.

The angle is deter$$anonymous$$ed by taking the player position away from the mouse position, that generates a vector 3 like (0,1,0) which would then generate the right angle to make my object rotate to point upwards where the mouse is, but when the camera has hit the boundary and the player ran to the edge of the map, the mouse would have to be around the middle of the camera to be in the same place as the player, even though the player is not in the middle but is on the far left of the camera. :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

camera zoom/moveing and object position 0 Answers

Issue with getting the mouse position in 3D and moving a gameobject on the y & z axis 0 Answers

How to rotate a sprite after using LookAt in 3D 0 Answers

How do I draw a raycast with my mouse position that is relative to the main camera? 0 Answers

Is there an easier way to do this? 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