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
1
Question by Jason Hamilton · Apr 23, 2011 at 11:58 AM · camerarotationpositionmousescreen

Mouse.position from center of player

Hey, Im working on a side scroller and the player rotates left or right depending on the position of the mouse in relation to the center of the screen. Heres the code;

var MousePosition = Input.mousePosition;
MousePosition.x -= Screen.width / 2;
MousePosition.y -= Screen.height / 2;
if(MousePosition.x < 0)
{
    transform.rotation = Quaternion.Euler(0,180,0);
}
else if(MousePosition.x > 0)
{
    transform.rotation = Quaternion.Euler(0,0,0);
}

Now what Id like to do is have the players position on screen (the camera has a Lerp smooth follow thing on it) offset were the rotation takes place, kind of like screen.width is local, if that makes sense.

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

Answer by Seregon · Apr 23, 2011 at 12:26 PM

You can get the players position on the screen using Camera.WorldToScreenPoint(). Once you have this you can easily compare it to your mouse position, hope this helps:

var MousePosition = Input.mousePosition; var PlayerPosition = Camera.main.WorldToScreenPoint(transform.position);

if(MousePosition.x < PlayerPosition.x) { transform.rotation = Quaternion.Euler(0,180,0); } else if(MousePosition.x > PlayerPosition.x) { transform.rotation = Quaternion.Euler(0,0,0); }

Comment
Add comment · Show 5 · 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 Bunny83 · Apr 23, 2011 at 01:10 PM 0
Share

I guess you want to write PlayerPosition.x inside the two if statements but beside that everything is ok.

avatar image Seregon · Apr 23, 2011 at 01:35 PM 0
Share

I hadn't spotted that, thanks. I've fixed the code now.

avatar image Jason Hamilton · Apr 24, 2011 at 03:01 AM 0
Share

Yay! it worked! I love you! I want your babies! etc! etc! one little thing I needed to divide PlayerPosition.x by Camera.main.pixelWidth / 2, where do I post that? do I just answer my own question?

avatar image Jason Hamilton · Apr 24, 2011 at 03:02 AM 0
Share

woops I meant to say subtract camer.main from Playerposition....

avatar image Seregon · Apr 24, 2011 at 10:30 PM 0
Share

I'm not sure I understand why you want to do that. I thought the only reason you subtracted half the width was so that you could compare the mouse position to the middle of the screen, and comparing the mouse position to the player position makes that unnecessary? Subtracting half the width from both the player position and mouse position would not affect the result. If I have misunderstood, please explain what your trying to achieve.

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

No one has followed this question yet.

Related Questions

rotate camera on mouse reaching edges 1 Answer

Rotate 3rd person Character according to mouse position 0 Answers

Need help on the 3ds max style camera control 0 Answers

Create camera transition from one point to top of object 1 Answer

How to set child position and rotation fixed respect parent? 0 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