- Home /
Question by
Anxo · Aug 07, 2014 at 04:12 PM ·
physicsoptimizationlag
Physics Optimization.
Hello. I am running a game with hundreds of "particles" (cyrcles) with a circle collider on them. Each "particle" has a script on it that I am trying to optimize as much as possible as it will be running on many many objects at the same time. This is what I got so far but if you notice anything where I can further optimize this class. Please let me know.
Thank you.
public class ParticleScript : MonoBehaviour
{
public bool alreadySuckedIn = false;
public bool alreadyCloggedIn = false;
public int PoolID = 0;
public float maxSpeed = 4f;
private int scaleDelay = 20;
public int WorthPoints = 0;
private ObjectPool objectPool;
private Rigidbody2D _myRigidbody;
public void Start ()
{
_myRigidbody = rigidbody2D;
}
public void FixedUpdate ()
{
float myVelocitySqr = _myRigidbody.velocity.sqrMagnitude;
if (myVelocitySqr == 0.0f)
return;
if (myVelocitySqr > maxSpeed) {
_myRigidbody.velocity = _myRigidbody.velocity.normalized * maxSpeed;
}
_myRigidbody.AddForce (RandomWind.WINDDIRECTION);
if (alreadyCloggedIn)
gameObject.rigidbody2D.Sleep ();
Vector3 myPos = transform.position;
if (myPos.x > 20f)
objectPool.Store (gameObject, PoolID);
else if (myPos.x < -20f)
objectPool.Store (gameObject, PoolID);
else if (myPos.y > 8f)
objectPool.Store (gameObject, PoolID);
else if (myPos.y < -8f)
objectPool.Store (gameObject, PoolID);
}
}
Comment