Touch control slowing game down

I’m moving my project over from Mac to iPhone now that iOS4 is out and Unity games are still being approved. If my tests go OK, I’ll buy Unity iPhone in the next few days.

I’m getting up to 1400fps on my 3 year old iMac, dropping to 300fps at the worst point with more enemies than normal on screen, so hopeful of good performance on the phone.

It was easy to get set-up on the iPhone as already an Apple developer for a non-Unity project. I haven’t tried Remote yet as testing for device performance.

My plan to use the accelerometer for movement has gone out of the window, as even if I sorted the jittering movement, the accuracy needed would be hard for the average user in my game layout. But I was upbeat as GetButtonDown and GetButton worked for fire and autofire without changes (but won’t be using them now), and the game ran fine even with 25 enemies on screen.

I am working on Plan B. Automatic autofire and touch for movement.

I added multiTouchEnabled = false; to my script. Made the autofire not need a button press. And changed the ScreenPointToRay (compared to the Mac version) line from:

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

to:

var ray = Camera.main.ScreenPointToRay(iPhoneInput.GetTouch(0).position);

This works, but the game is running very slowly, sometimes even freezing temporarily, and that is only with the player autofiring, no enemies.

Am I doing it right? Are there any known issues causing this? Any suggestions for getting around this? Is there a way to tell what framerate I am getting (or potential framerate if it is locked to a particular rate)?

I’m using Unity 1.7. Building to 3.1.3 in Xcode. Running on an iPhone 3G with 3.1.3.

There must be something else causing your performance issues, I use touch driven raycasts all the time with no problems. You might try using a layer mask on the raycasts to at least limit the number of colliders in the scene that it is testing against (not sure if that could cause such severe slowdown, just guessing).

That is what is so puzzling. If I understand my code right, it is only testing against a Plane (it is based on something I read on the internet, and is my least understood code).

#pragma strict

private var targetPlane : Plane;

// other vars

function Awake () {
        multiTouchEnabled = false;

	// other set-up

	targetPlane = Plane(Vector3().forward, 0);
}

function Update () {	

	// autofire code

	var ray = Camera.main.ScreenPointToRay(iPhoneInput.GetTouch(0).position);
	var dist : float;
	if(targetPlane.Raycast(ray, dist)) {
		var pos = ray.GetPoint(dist);

		// movement based on pos

	}
}

I had been using FixedUpdate, as it is usually called less often, but I have got an improvement by changing to Update. Presumably my framerate had dropped below the FixedUpdate frequency.

What you’ve posted there looks fine to me, nothing there should be causing any severe slowdown. Is it possible you’re instead experiencing hitches as assets are loading for the first time? Any large sounds or textures can cause nasty performance hits when they are loaded into memory on older devices, which only occurs when they are first seen by your camera or heard.

Edit: and yea, Update is the way to go. You don’t want to put any player input in Fixed Update, it tends to behave strangely.

You should only call the raycast if there is a touch. Check your console, I bet a tonne of debug logs are getting pumped out there due to passing null into the function when there are no touches.

Try this instead

if(iPhoneInput.touchCount > 0) {
   var ray = Camera.main.ScreenPointToRay(iPhoneInput.GetTouch(0).position);
   var dist : float; 
   if(targetPlane.Raycast(ray, dist)) { 
      var pos = ray.GetPoint(dist); 
   }
}

If you need auto-firing in your code, just store the latest touch in the script, and check for an update on it instead. Then just use the local copy of the variable to pass into the ScreenPointToRay function.

there is also a performance hit of a couple of fps per touch as well Ive found in my various projects.

Thank you everyone for your help.

Murcho’s suggestion about debug logs lead me to enabling Strip Debug Symbols in the Build settings. Things ran much more smoothly after that.

It still seemed a bit jerky though. I tried changing the FPS in Xcode to 60 and things appeared smoother. A change to 120FPS was even better.

So it seems after stripping the debug symbols that the game was flying, but the restricted frame rate was not showing it.