x


How to rotate a character facing direction of movement like sonic the hedgehog

Hey guys, my title pretty much sums it up. I've been stuck on this forever now. I keep breaking and coming back to it but I really want to solve this. Hopefully someone can help me out. I want to consistently rotate my character like a constant forward roll in his direction of movement, just like in sonic the hedgehog. I've got his direction of movement working right and his roll movement working but I can't for the life of me figure out how to have him face his move direction while he rolls forward. Obviously I can't use transform.LookAt because he's consistently rolling and his LookAt point will change constantly. Here is what I've got... any ideas?

    #pragma strict
var rotateSpeed: float = 60;
private var lastPos: Vector3;

function Update(){
var moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,0); 
var dist = transform.position - lastPos;
lastPos = transform.position;
var angle = dist.x * rotateSpeed;


if(moveDirection.x > 0){
print("moving right");
transform.Rotate(angle, 0, 0);
}
if(moveDirection.x < 0){
print("moving left");
transform.Rotate(-angle, 0, 0);
}
}
more ▼

asked Apr 17 '12 at 03:52 AM

stingman gravatar image

stingman
562 24 29 33

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

2 answers: sort voted first

Maybe make him the child of an empty gameobject, and then the character can rotate and stuff in local space, while the parent defines the facing direction.

more ▼

answered Apr 17 '12 at 04:04 AM

Lttldude gravatar image

Lttldude
1.2k 1 2 7

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

lttldude9 you made my night. I can't believe the solution was so simple. I still gotta tweak some stuff but yup this totally works.

feel free to post an answer and ill +thumb up it

more ▼

answered Apr 17 '12 at 04:21 AM

stingman gravatar image

stingman
562 24 29 33

I'm new to http://answers.unity3d.com and never asked a question before, but I think when someone answers your question correctly you are suppose to actually select that answer as correct. From the FAQ: "Click the check-mark icon to make it green." Idk, tell me if that makes sense.

Apr 17 '12 at 02:26 PM Lttldude
(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:

x2163
x1367
x724
x286
x59

asked: Apr 17 '12 at 03:52 AM

Seen: 946 times

Last Updated: Apr 17 '12 at 02:26 PM