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 sjoerd216 · Dec 31, 2013 at 12:12 PM · unity 2ddirectionfacing

Finding relative mouse position

Hi all!

I am having some problems with my mouse relative positions. I am working in the Unity 2D envoirement and I have this transform which moves to the left and right, and I wanted to let the character face the direction of the cursor. But I can't figure out how

This is what I got now:

 if(Input.mousePosition.x > transform.position.x){transform.localScale = Vector3(1,1,1);}
 if(Input.mousePosition.x < transform.position.x){transform.localScale = Vector3(-1,1,1);}{transform.localScale = Vector3(-1,1,1);}

This seems to work in some way, but the transform only changes direction when the cursor is all the way to the left of the screen. What I would like to see, is that the transforms direction changes when the cursors position is smaller/larger the transform.

Does anyone know how to fix this?

Thanks in advance!

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 Bunny83 · Dec 31, 2013 at 12:15 PM 0
Share

@sjoerd216: You posted this question 4 times. Your questions were stuck in the moderation queue to prevent spam$$anonymous$$g on this site. Once you have 15+ $$anonymous$$arma you can post directly, until then please be patient until someone published your post.

You might want to read the site FAQs and watch the new tutorial video.

avatar image sjoerd216 · Dec 31, 2013 at 04:55 PM 0
Share

Ow yeah sorry for the spam. I thought that I posted it only once. Sory about that

2 Replies

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

Answer by blueteak · Dec 31, 2013 at 06:05 PM

I would look at

Camera point to world point and maybe Transform.LookAt()

Get the mouse position in world space (you are getting screen space, which is the main problem) and make your object Transform.LookAt(It) or build your own function to rotate to face that point.

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 sjoerd216 · Dec 31, 2013 at 06:12 PM 0
Share

The mouse position in world space works for me. After some editing it finally works. Thanks!

avatar image blueteak · Jan 01, 2014 at 03:31 AM 0
Share

Awesome, glad I could help!

avatar image
1

Answer by adnirjhar · Jan 01, 2014 at 05:28 AM

Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

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

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

addForce to Character in direction of other object 1 Answer

Help with my "CanHitTarget" function 2 Answers

How to snap a model to the direction being moved in, without also rotating the character controller 3 Answers

How do i shoot in the direction im facing? 3 Answers

[Game like Pong] Trying to change direction with a boxcollider 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