Code Slows Mobile Performance Substantially

Hi there, I am using this code to perform some drag and movement features on the iOS. The reason I am posting here is that hopefully someone can help me out (seems like more traffic and this is an overall performance question rather than anything else).

For some reason, this script, which deals with player rotation via a D-Pad, if pressed, over a long period of time (30 seconds) with out a release, will drop fps by 10, frames, then more and more, gradually loosing steam and hovering at about 12fps. I can see no reason why. I am thinking perhaps my use of while(), but ultimately I think garbage collection must be an issue, however I am unsure as to why. Can anyone instruct me as to why this code is terrible for a mobile? * I am looking at Touch now to replace raycast but I was shocked at the performance drop.

function Update()
{
   if (Input.GetMouseButtonUp(0)){
    	///CANCEL THE ABILTY TO FIRE (* handled in determineInputMethod())
    	pressingDown = false; 
    }
    else if (Input.GetMouseButton(0)){	
    	theTouch =Input.mousePosition;
    	ray = cam.ScreenPointToRay(theTouch);
        if (Physics.Raycast(ray, hit)){
			if (hit.collider == thisCollider){		
				pressingDown = true;
				handleInputMethod();
				return;	
			}
        }
    }	
}


function handleInputMethod()
{
	while(pressingDown){
		
		handleRotation();
		yield;
	}
}



function handleRotation()
{
	///gather rise and run of input pos with centerNodePos
	inputPos = Input.mousePosition;
	targetNodePos = cam.ScreenToWorldPoint(inputPos);
	targetNode.position = Vector3(targetNodePos.x, targetNodePos.y, -40);
	targetNodePosLocal = targetNode.localPosition;
	
	centerNode.LookAt(targetNode);
	brain.rotation = centerNode.localRotation;
	
	if(targetNodePosLocal.x < 0){////face brain left
		if(brain.localScale.x != -1){			
		brain.localScale.x = -1;
		brainLocalScale = -1;
		}
	}
	else if(targetNodePosLocal.x > 0){////face brain left
		if(brain.localScale.x != 1){
			brain.localScale.x = 1;
			brainLocalScale = 1;
		}
	}	
yield;						
}

handleInputMethod is clearly a coroutine. That means it will continue to run as long as pressingDown is true.

In Update you invoke handleInputMethod each frame the mouse is held over this object. Invoking a coroutine in unityscript is the same as if you called StartCoroutine on it. So each frame you spawn yet another coroutine, leading to a continous slowdown.

Changing line 7 to Input.GetMouseButtonDown may actually work as you intended. By only starting the coroutine only the frame the mouse is pressed as opposed to any frame the mouse is down while hovering over your object.

Thanks!
So, I removed the call to “handleInputMethod()” and instead, onMouseDown() used InvokeRepeating(handleRotation), at a frequency of 0.01 seconds, the rotation is smooth and has no effect on the fps on an iPod4thGen (*perhaps that should be posted in iOS). Anyhow, it is working fine, so thank you.

I do have a question tho as to what was exactly happening. You said…

“Invoking a coroutine in unityscript is the same as if you called StartCoroutine on it. So each frame you spawn yet another coroutine, leading to a continous slowdown.”

So, by using call function, then in that function use while(), and in while call another function, I was exponentially calling functions? And even calling functions upon functions!?!?!