# Rotating my character only while moving in a 2D plane

 0 Hi everyone. I've spent about 6 hours now trying to figure this out... I've tried so many different versions of code and nothing seems to work. I'm creating a 2D side scroller and when my character moves I want him to rotate around. Think of sonic the hedgehog when sonic spins and rolls. Or even just think of a sphere that rotates in the direction you move it. I want my character to spin at a very fast rate. I've tried adjusting torque and force to my rigidbody over every axis and all that does is make him very choppy; it does not affect his rotation at all. I'm doing something wrong wrong and reaching out in hopes someone might be able to lend a hand. Anyone have any solutions, code snippets or tips using Java? more ▼ asked Jan 10 '12 at 11:27 PM stingman 562 ● 24 ● 29 ● 33 are you using the position and rotation in the same control object? Jan 10 '12 at 11:34 PM 3D-Magic-VR yes i'm using position and rotation in same control object... i'm using a modified version of the platform controller Jan 11 '12 at 03:24 AM stingman add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 You should use the actual velocity to calculate the rotation, and use transform.Rotate to make it spin. If your character is a rigidbody, you can use: ```var factor = 120; // degrees per meter function FixedUpdate(){ var angle = rigidbody.velocity.x * Time.deltaTime * factor; transform.Rotate(0, 0, angle); } ```This will rotate the character in the direction it's moving in the X axis (invert angle sign if it spins to the wrong side). You can adjust factor to get the spinning velocity you want. NOTE: Set rigidbody.freezeRotation=true at Start to avoid interference from physics rotational effects. EDITED: Well, since you're using a CharacterController, things must be somewhat different: you must child your model to the character object and rotate it proportionally to the distance travelled each Update (model script): ```var speed: float = 120; private var lastPos: Vector3; function Update(){ var dist = transform.position - lastPos; lastPos = transform.position; var angle = dist.x * speed; transform.Rotate(0, 0, angle); } ``` more ▼ answered Jan 10 '12 at 11:48 PM aldonaletto 41.4k ● 16 ● 42 ● 197 I understand what you are telling me to do but perhaps there is something conflicting with this code already written. I'm using the standard character controller script (slightly modified) to control my character. When you note to set the rigidbody.freezeRotation = true at start... do you declare this at top of file or under the Awake function? Jan 11 '12 at 03:26 AM stingman Character Controller mixed with a rigidbody doesn't work very well from what I've read... but as for the rotation freeze, you can set that in the inspector panel on your rigidbody contraints , or in Start() like @aldonaletto suggested. Jan 11 '12 at 03:30 AM Lo0NuhtiK I had a feeling this was the issue. I'll just write my own character control script. Was trying to cut a few corners but it will probably be easier if I do it this way. Thanks for the help guys Jan 11 '12 at 03:44 AM stingman The code above actually rotates the character proportionally to the distance walked. To make this work with a CharacterController, you must measure the distance walked between Updates and rotate the character proportional angle - but there's a big problem: rotating a CharacterController is a recipe for disaster. To avoid problems, you must child the model to the character and rotate only the model. I'll edit my answer to include this CharacterController alternative. Jan 11 '12 at 11:04 AM aldonaletto Thank you for the tips. So I made the character mesh the child to the character object and I created a new script for the character to rotate and applied it to the character object. I changed the vector 3 to (angle, 0, 0) so it rotates the way I wanted it to but other than that the code you provided for me is definately helping me get on the right track! The only issue is that my character now stops rotating when he turns 90 degrees on his axis and just moves along at that angle. The character does not continually rotate. I'm sure there's a quick fix and I'll try and figure out a way for him to continually rotate... basically whatever direction he moves I want him to continually rotate when he's being controlled... not just rotate 90 degrees and stay fixed in that position hehe... I probably wasn't too clear but you've been a huge help so far! Thank you! Jan 11 '12 at 07:54 PM stingman add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 fyi what aldonaletto wrote works correctly for anyone trying to do this w/ a character controller. Just make sure you adjust the rotate smoothing to 0 and the speed smoothing to a small value (I set it to 0.2) The roll effect works like a charm! :) Thanks again! more ▼ answered Jan 14 '12 at 05:03 AM stingman 562 ● 24 ● 29 ● 33 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x2166
x1873
x1791
x1367
x1036

asked: Jan 10 '12 at 11:27 PM

Seen: 1694 times

Last Updated: Jan 14 '12 at 05:03 AM