Hi there,
I have this script which is a controller for a dPad. It rotates and fires bullets from my player. I am writing looking for some help. The code works perfectly, however, when in use, at the moment, I am experiencing a 5fps drop on mobile (iPod 4th Gen - lowest of the low in terms of power, but wanting to get it optimized on this first).
So, I am looking for some community members, perhaps some elder statesmen, to see if they can point out, anything obvious I am doing that might be causing such a drop. I am thinking it could be a number of things. Scaling, using rigidbodies velocity to propel multiple bullets. Running a for() loop to check for active pool members. Etc.
Perhaps I have posted out of haste, but if you have a minute, I would love to hear what you would do.
Thanks,
Ren
using UnityEngine;
using System.Collections;
////THIS SCRIPT HANDLES PLAYER ROTATION AND GUN FIRE.
////PLAYER IS A 2D OBJECT THAT CAN ROTATE FIRE 360 DEGREES.
////MOUSE/TOUCH INPUT IS HANDLED BY EZGUI.
public class dPadRotation : MonoBehaviour {
///VARS FOR ROTATION CNTRLS
public Camera cam;
Vector2 theTouch;
Ray ray;
RaycastHit hit;
bool pressingDown;
Vector3 targetNodePos;
Vector2 inputPos;
public Transform centerNode;
public Transform targetNode;
public Transform brain;
Vector3 targetNodePosLocal;
///VARS FOR GUN CNTRLS
int fireCount;
public Transform Nozzle1;
public Transform Nozzle2;
Vector2 nozzleTrajectory;
public GameObject[] bulletPool;
GameObject bullet;
Transform bulletTransform;
Rigidbody bulletRigidbody;
int bulletCount;
int bulletPoolLength;
public float speed;
Vector2 aPt;
Vector2 bPt;
float rise;
float run;
float radians;
float degree;
float nozzleRise;
float nozzleRun;
Vector2 bulletTrajectory;
int bounceStage; ///handles the kickback anim from the gattling.
public Transform bounce2;
public Transform bounce1;
int turnOverCount;
int brainLocalScale;
int gunFiring;
int dragCount; ///eases computation of rotation frames
void Start(){
// Get a reference to our control script:
IUIObject control = (IUIObject) GetComponent(typeof(IUIObject));
// Tell the control to call our method when it receives input:
control.AddInputDelegate(MyDelegate);
bullet = bulletPool[0];
bulletPoolLength = bulletPool.Length;
}
///EZGUI FEATURE, ALLOWS FOR ME TO MONITOR ANY CHANGE IN TOUCH INPUT
void MyDelegate(ref POINTER_INFO ptr)
{
///IF PLAYER IS DRAGGING WHILE TOUCHING THIS BTN, GUN FIRE AND ROTAITON ARE HANDLED
if(ptr.evt == POINTER_INFO.INPUT_EVENT.DRAG){
inputPos = ptr.devicePos;
RotateBrain();
///this method insures us that we do not initate gun fire every frame.
fireCount++;
if(fireCount == 3){
fireCount = 0;
getBullet();//begins firing weapon process.
}
}
else if(ptr.evt == POINTER_INFO.INPUT_EVENT.MOVE_OFF)
Debug.Log("Move Off + " + Time.time);
else if(ptr.evt == POINTER_INFO.INPUT_EVENT.RELEASE_OFF)
Debug.Log("Release off !");
}
////ROTATE BRAIN -
///1 System works by setting a transform in the world space of touch pos (if this btn being used)
///2 then a center node (transform is rotated via lookAt() to look at the finger transform.
///3 my player is then set to the same rotation of the centerNode.
void RotateBrain(){
targetNodePos = cam.ScreenToWorldPoint(inputPos);
targetNode.position = new Vector3(targetNodePos.x, targetNodePos.y, -40);
centerNode.LookAt(targetNode);
brain.rotation = centerNode.localRotation;
targetNodePosLocal = targetNode.localPosition;
////FLIP PLAYER
////2D game, must account for "flip of player" (face left, face right)
Vector3 brainScale = brain.localScale;
if(targetNodePosLocal.x < 0){////face brain left
if(brainScale.x != -1){
brainScale.x = -1;
brain.localScale = brainScale;
brainLocalScale = -1;
}
}
else if(targetNodePosLocal.x > 0){////face brain right
if(brainScale.x != 1){
brainScale.x = 1;
brain.localScale = brainScale;
brainLocalScale = 1;
}
}
}
////THE GUNFIRE METHODS
////BEGING THE CYCLE OF BULLET GATHERING FROM POOL AND FIRING.
void getBullet(){
if(bulletCount >= bulletPoolLength){
turnOverCount++;
resetBullets();
return;
}
if(bulletCount <= bulletPoolLength){
///means we should still have bullets left. Have not run thru pool.
//check to see if assigned bullet is active.
bullet = bulletPool[bulletCount];
if(bullet.active == false){
bulletTransform = bullet.transform;
bulletTransform.position = Nozzle2.position;
bullet.active = true;
determineTrajectory();
bulletCount++;
return;
}
else if(bullet.active == true){
//we must find an inactive bullet
for(int b = bulletCount; b < bulletPoolLength; b++){
bullet = bulletPool[b];
if(bullet.active == false)
bulletTransform = bullet.transform;
bulletTransform.localPosition = Nozzle2.position;
bullet.active = true;
determineTrajectory();
bulletCount++;
return;
}
}
}
}
}
void determineTrajectory(){
Debug.Log("determineTrajectory() " + Time.time);
aPt = Nozzle2.position; // touch position
////print("aPt " + aPt);
bPt = Nozzle1.position; //playerObject position in screen space
////print("bPt " + bPt);
nozzleRise = aPt.y - bPt.y; /// rise
nozzleRun = aPt.x - bPt.x; /// run
bulletTrajectory = new Vector2(nozzleRun, nozzleRise);
bulletTrajectory.Normalize();
bulletTransform.rotation = Nozzle2.rotation;
scaleBullet();
}
void scaleBullet(){
if(brainLocalScale == -1){///facew bullet left
bulletTransform.localScale = new Vector3(3.5f, -3.5f, 3.5f);
}
else if(brainLocalScale == 1){//face bullet right
bulletTransform.localScale = new Vector3(-3.5f, -3.5f, 3.5f);
}
fireBullet();
}
void fireBullet(){
bulletRigidbody = bullet.rigidbody;
bulletRigidbody.freezeRotation = true;
bulletRigidbody.velocity = new Vector3(bulletTrajectory.x * speed, bulletTrajectory.y * speed, 0);
}
}