- Home /
Flip 2d character left/right
I need to flip a 2d character in a 3d world to look right/left dependent on the direction they move relative to the screen. So if they wander in a left direction relative to the screen it will flip to look left and vice e versa.
How do I define an objects position relative to the screen so I can tell if it moved in a left/right direction from it's last 'relative to screen position'?
Thanks for any help!
Answer by robertbu · Aug 23, 2014 at 05:41 AM
Assuming you have a standard 2D setup, just subtract the previous position of the object from the current position of the object. If the x of the resulting vector is negative, the movement is to the left. A positive value, and the object is moving to the right.
$$anonymous$$y set up an npc object nested in an empty gameobject that has the movement script on it. the nested npc is the 2d image that needs to face left/right while pointing at the camera. I've tried this
TempPos = Camera.main.WorldToScreenPoint(target.position);
if(lastPos.x - TempPos.x < 0)
innerNpc.transform.localScale.x = -1;
else
inner.transform.localScale.x = 1;
but it only flips occasionally and still in the wrong direction. What am I missing?
This code fragment does not show how and when you are setting lastPos.
Try having it so everytime you change direction run this:
npc.transform.localScale.x *= -1;
I've not tested it but I think that works
Your answer
Follow this Question
Related Questions
UI RectTransform Position && Screen Resolution 2 Answers
Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 1 Answer
Fitting in a turn around animation (Alucard-like) in a transition from facing right to the left (2D) 1 Answer
Flipping an 2D object with joints 2 Answers
Automatically adjusting the camera view 0 Answers