- Home /
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!
@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.
Ow yeah sorry for the spam. I thought that I posted it only once. Sory about that
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.
The mouse position in world space works for me. After some editing it finally works. Thanks!
Answer by adnirjhar · Jan 01, 2014 at 05:28 AM
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Your answer
Follow this Question
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