Better Method To Move A Coin

Hi there,
I am making a side scroller. In it, you can collect coins by simply running into them. I am writing because when I have a mass amount of coins in screen, my frame rate can drop between 4-7 frames. So, on mobile, at a low point I am looking at 23.

I am quite sure the issue is how I move collected coins up into the hud. OnTriggerEnter, I pass this function. I am wondering, can anyone suggest something that would be easier on the engine?

Thanks.

function coinCollected(){
	///having the coin collected, we must pass this info to the coinManager
	///also, we must show here, this coin collection animation.
	while(thisTransform.position != coinHudPos){
		thisTransform.position = Vector2.MoveTowards(thisTransform.position, coinHudPos, coinSpeed * Time.deltaTime);
		yield;
	}
}

edit, I just did two tests. Both eliminated the above function, however, one removed the colliders on the coins. The second did not. THe removed colliders played pretty much at 30fps (mobile). With colliders, using OnTriggerEnter, I still dipped to about 26-27fps.

Any suggestions?

Edit.
This is solved.
The issue was not with moveTowards, but a class of object which, served no purpose, but surprisingly were taking up memory.

There are potential problems in your movement code - if the movement “overshoots” the position (coinspeed * movement delta > distance), then it will ping-pong around, possibly never ending your loop. You should calculate the distance from the coin to the destination, calculate the suggested movement, and check to see if the coin is closer. If so, just set the coin to the position, otherwise do your move as is.

Hm. Good point.
Thanks.

Is there a better alternative to yield in this case?

My gut feel (without knowing all the circumstances) is that the code problem above is probably contributing to the slowdown as you generate more and more loops. I can’t imagine the yield code would be bad. It’s way more efficient than putting an update function on each coin and doing it that way. You might want to make sure you aren’t getting a lot of “OnTriggerEnter” calls, and generating new loops.
If you don’t want to move it every frame, then you can move it every 10th of a second or something by using yield WaitForSeconds (0.1); (also need to change your time.deltaTime to 0.1)

Awesome.
No, I just remeber reading yield, will cause a frame pause. I was wondering if break or something is preferred. Also, I did edit the first post as, the real cause was a completely unrelated class of object, I thought were having no impact.