side scrolling, latency lag

i need help , for this code this code have latency in build game, 0,5 lag

    var speed : float = 6.0;
    var jumpSpeed : float = 8.0;
    var gravity : float = 20.0;
    private var moveDirection : Vector3 = Vector3.zero;
    function Update() {
       
        
      var controller : CharacterController = GetComponent(CharacterController);
        if (controller.isGrounded) {
            // We are grounded, so recalculate
            // move direction directly from axes
            moveDirection = Vector3(Input.GetAxis("Horizontal"),0,0);
            
            //moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            
          if(moveDirection.sqrMagnitude > 0.1)
          transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(moveDirection),1);
         
          
          
              if (Input.GetButton ("Jump")) {
                moveDirection.y = jumpSpeed  ;
            }
        }
        // Apply gravity
        moveDirection.y -= gravity * Time.deltaTime;
        
        // Move the controller
        controller.Move(moveDirection * Time.deltaTime);
    }

or need code side scrolling

sorry for bad english

GetComponent is slow, and you’re calling it in every frame of the Update() loop (in the line var controller : CharacterController = GetComponent(CharacterController);).

Instead, cache a reference to the character controller by declaring the controller variable outside the loop, and getting a reference just once in the Start() function, like this:

var controller : CharacterController;

function Start() {
   controller = GetComponent(CharacterController);
}

function Update() {

   // No need to GetComponent any more - we can just use controller already
   if (controller.isGrounded) { etc. etc. }
}