x


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);
        }
    } 
more ▼

asked Apr 03 '11 at 02:44 PM

Xatoku gravatar image

Xatoku
152 73 81 88

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.)

more ▼

answered Apr 03 '11 at 03:07 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

Thanks a ton dude! Works Perfectly!

Apr 03 '11 at 03:32 PM Xatoku

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!

Mar 07 at 09:54 AM A Digital Goodie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3330
x3002
x1044
x1037
x52

asked: Apr 03 '11 at 02:44 PM

Seen: 1187 times

Last Updated: Mar 07 at 09:54 AM