- Home /
2D Character Flip
Hey guys. I've been working on this 3d sidescrollr and managed to get the character flipping properly based on which way hes moving, but the problem is that if he runs towards the left, he will face left, but run right. Any help here?
var walkSpeed = 30;
function Update () { if(grounded) { moveDirection = new Vector3(0,0,Input.GetAxis("Horizontal")); transform.LookAt(transform.position + moveDirection); if(isMoveable == true){ moveDirection = transform.TransformDirection(moveDirection); moveDirection *= walkSpeed; moveStatus = "idle"; } if(moveDirection != Vector3.zero && isMoveable == true) { animation.CrossFade("run",0.2); isMoving = true; } else { isMoving = false; animation.CrossFade("Take 001",0.2); } }
Answer by Jesse Anders · Apr 03, 2011 at 03:07 PM
Try removing this line:
moveDirection = transform.TransformDirection(moveDirection);
And see what happens. (If 'moveDirection' is already in world space, there shouldn't be any need to transform it. I don't see anything in your code that specifies what space moveDirection is in, but I'm guessing it's world space.)
Wow! Used so many hours trying to figure this one out as well, and it was all a matter of removing one single line. Damn... Thanks!
Answer by Fubiou · Sep 07, 2015 at 07:32 AM
Hey, I found a simple way to flip the 2d character with a simple line:
transform.localEulerAngles = transform.eulerAngles + Vector3(0,180,-2*transform.eulerAngles.z);