Hi all,
I always try to give less reference calling for Unity Engine. In a post ReJ suggested that more script, more entry point and more load on Unity Engine. Now, I’m facing a problem that is happening due to script reference I think. I need your help to overcome this. Specially from ReJ.
Here it is: In my game I have a gun which is creating a bullet GO while it receiving Shoot MSG. This is using my own MSG passing system and not costly I assure you. I can represent my gun controller in that way:
var bulletPrefab : GameObject;
var theReticle : GameObject;
var fireRate : float = 0.2;
private var nextFire : float = 0.0;
function ReceiveMessage ( msgInfo : Message ){
if ( msgInfo == Message.Shoot ){
if ( Time.time > nextFire ){
dirToMove = (theReticle.transform.position - transform.position).normalized;
var thisBullet : GameObject = Instantiate (bulletPrefab, transform.position, Quaternion.LookRotation ( dirToMove) );
nextFire = Time.time + fireRate;
}
return;
}
}
While it’s receiving a msg from game controller that it’s need to fire then it’s creating an Instance of the bullet by setting position and direction. I also controlled the fire rate by timer as you can see.
Now the bullet:
var timeOut : int = 5.0;
var bulletSpeed : int = 100;
private var targetToLook : Transform;
private var dir : Vector3;
function Start () {
Invoke("Kill", timeOut);
targetToLook = Camera.main.transform;
dir = transform.forward;
}
function Update () {
transform.LookAt ( targetToLook );
transform.position += (dir * bulletSpeed * Time.deltaTime);
transform.eulerAngles.z += 2000 * Time.deltaTime;
}
This GO has a child GO which is containing a plane. So, I’m just make it to look at the camera to be a billboard. Also moving forward to by a speed. We may optimize the Update function [you can suggest me].
Now, when I’m pressing the fire button from UI [I’m using a single Input manager which is optimized and it’s not costly - Thanks to ReJ for it in another thread], the FPS is dropping by 4-5 FPS at least.
This is the state that I’m getting while not firing the bullet
and this is the state while I have some bullet that already fired [may be 5-8].
So, the major difference that I’m getting is “mono-scripts> update: 14.5”
Some other also but this one is major I think.
Can anyone help me out the way which will help me not to drop my precious FPS?
Question #2:
Consider a script :
and another one is:
The main difference is, in the second script I have the Update function. What cost to Unity Engine of these scripts? Are they same to Unity or 2nd one is more due to Update function? I’m my opinion 2nd one should be more costly.
Any opinion? I think this will help other a lot in case of dynamic object creation.
Thanks to all.