Character rotation problem!

Hi! I'm new to scripting and working hard to learn Javacript. I'm writing a movement script for a 2D-sidescroller. I made this with scripts from tutorials and what I've found in forums. Movement works exactly how i want it to, but i cant get the controller to rotate along its y-axis when turning to walk the other way (You will find my attempt at the end of the script).

I would really appreciate it if you could help me out here!:D

Sigurd

Movement.js

var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded = false;

class ControllerMovement {

var speed = 6.0;
var jumpSpeed = 8.0;
var rotationSmoothing = 10.0;

@System.NonSerialized
var direction = Vector3.zero;

// Are we jumping? (Initiated with jump button and not grounded yet)
@System.NonSerialized
var jumping = false;

 // This keeps track of our current velocity while we're not grounded?
@System.NonSerialized
var inAirVelocity = Vector3.zero;

}

var movement : ControllerMovement;

private var controller : CharacterController;

function FixedUpdate () {

transform.position.z = 0;

}

function Update() {
if (!grounded) {
  moveDirection.x = Input.GetAxis("Horizontal") * movement.speed;     
}
// If grounded, zero out y component and allow jumping
else {
  moveDirection = Vector3(Input.GetAxis("Horizontal") * movement.speed, 0.0, 0.0);

  if (Input.GetButton ("Vertical")) {
     moveDirection.y = movement.jumpSpeed;
  }
}
moveDirection = transform.TransformDirection(moveDirection);

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime) & CollisionFlags.CollidedBelow) != 0;

// Set rotation to the move direction   
if (movement.direction.sqrMagnitude > 0.01)
    transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime *
    movement.rotationSmoothing);

// We are in jump mode but just became grounded
if (controller.isGrounded) {
    movement.inAirVelocity = Vector3.zero;
    if (movement.jumping) {
        movement.jumping = false;
        SendMessage ("DidLand", SendMessageOptions.DontRequireReceiver);

        var jumpMoveDirection = movement.direction * movement.speed + movement.inAirVelocity;
        if (jumpMoveDirection.sqrMagnitude > 0.01)
            movement.direction = jumpMoveDirection.normalized;
    }

 }

 }

 @script RequireComponent(CharacterController)

Figured it out myself:D

  1. Take the PlatformerController.js from the 2D Sidescroller-tutorial

  2. Add: private var grounded = false;

  3. Right under "// Grounded controls", replace; "if (controller.isGrounded)" with "if (!grounded)".

Play with the settings to get the desired movement. This gives sort of ( not exactly the same because of the extra hight-jump) movement as in the script above, but with rotation.

Sigurd