Lag making player jump

Hi Everyone,

My character is moving fine when there is limited things on the screen (a few spheres rotating), but when I add more, than 5 spheres or add a particle effect to one of them I get severe lag of the character when it moves (it jumps around the target position).

I was wondering if anybody could see anything in my code that could be changed to avoid the jumping or let me know a way to reduce the lag without taking objects out.

function CharacterControl()
{
    var count : int = iPhoneInput.touchCount;   
    if(state == ControlState.TrackingMove)
    {
        var touch : iPhoneTouch = iPhoneInput.GetTouch(0);

        if(touch.position.y > firingBorder  touch.position.y < 450)
        {

            var ray = cam.ScreenPointToRay( Vector3( touch.position.x, touch.position.y ) );
            var hit : RaycastHit;
            if( Physics.Raycast(ray, hit) )
            {
                var touchDist : float = (transform.position - hit.point).magnitude;
                if( touchDist > minimumDistanceToMove )
                {
                        targetLocation = hit.point;
                }
                moving = true;
            }
        }
        else
        {
            ResetControlState();    
        }

    }

    movement = Vector3.zero;
    rotate = Vector3.zero;

    if(state == ControlState.MovingCharacter)
    {
    if( moving )
    {
        // Move towards the target location
        movement = targetLocation - thisTransform.position;
        movement.y=0;
        movement.z=0;

        var dist : float = movement.magnitude;
        if(movement.x < 0)
        {
            rotate = Vector3(0,0,25);
        }

        if(movement.x > 0)
        {
            rotate = Vector3(0,0,-25);
        }
        if( dist < 1 )
        {
            moving = false;
            ResetControlState();
            rotate = -rotate;
        }
        else
        {
            movement = movement.normalized * speed;
        }
    }

    movement += velocity;       
    movement += Physics.gravity;
    movement *= Time.deltaTime;

    // Actually move the character
    character.Move( movement );
    character.transform.Rotate(rotate);
    velocity = Vector3.zero;
    }
}

Thanks

Chris

One optimisation that springs to mind is with the use of Vector3.magnitude. This innocent property is actually quite slow because it involves a square root. For the touchDist value, you could use the square magnitude property instead, and compare it against the square of the threshold distance:-

var touchDist : float = (transform.position - hit.point).sqrMagnitude;

if( touchDist > minimumDistanceToMove * minimumDistanceToMove) {
  ...

This approach gives the same result but avoids the square root.

Lower down, you calculate movement.magnitude but then use movement.normalized later on (normalisation also involves the magnitude operation and therefore a square root). You could normalise the movement vector by dividing it by the length you have already calculated:-

var dist : float = movement.magnitude; 
  ...
movement = movement / dist * speed;

You can save two of the square roots and so improve the efficiency somewhat. However, it’s quite possible that other things are slowing the script down as well. Perhaps you could you post the whole script, in particular the Update function where CharacterControl is called.