how do I sync movement with mouselook

I’m making a mobile game. And when I walk and lookaround everythings is ok but if I turn 180 degrees the movement is not synced so when I go forward, I go backwards! can someone please help me out with this? :slight_smile:

I’ve a capsule that the FPSwalker script is attatched to and a camera that is a child of it with the touchlook script!

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

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

function FixedUpdate() {
	var joy = VCAnalogJoystickBase.GetInstance("moveStick");
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(joy.AxisX, 0, joy.AxisY);
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

and heres the touchlook (this one is from unify wiki in C#)

        if (Input.touches.Length > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Moved)
            {
                Vector2 delta = Input.touches[0].deltaPosition;
                float rotationZ = delta.x * sensitivityX * Time.deltaTime;
                rotationZ = invertX ? rotationZ : rotationZ * -1;
                float rotationX = delta.y * sensitivityY * Time.deltaTime;
                rotationX = invertY ? rotationX : rotationX * -1;
 
                transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
            }
        }

thanks :slight_smile:

Hahah I just woke up and solved it :smiley:
change this: moveDirection = transform.TransformDirection(moveDirection);
to this: moveDirection = cam.transform.TransformDirection(moveDirection);

also add var cam : GameObject; and add the camera to it! :slight_smile:

so now it’s using cameras Transform direction! :slight_smile: