- Home /
C# - Raycast Bullets Object Pooling (please help)
Hello,
i have a weapon script and it is very slow- because i use Instantiate to create Bullet Holes. I searched for Object Polling and i found thinks but i dont know how to implement them into my script :( Please help me!! Heres my script- why is it so slow- could i optimize at some other points too?
public float maxDist = 1000000000f,damage;
public GameObject woodImpact, metalImpact, dirtImpact, concreteImpact, vegetationImpact, body, deformImpact, physmag;
public float floatInFrontOfWall = 0.001f;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
private bool canfire = true, isreloading = false, isshooting = false;
public float reloadtime;
public AudioSource weaponsource;
public AudioClip shotsound;
public AudioClip reloadsound;
public int magsize;
public float force, deformingforce, forceoffset;
public Transform shotcreator, magspawner;
private int bulletsinmag;
public float accuracy;
[HideInInspector]
public Quaternion originalPos;
private Kerbo_Metal_Deform deformer;
public Animation weaponanim;
public float mdd;
void Start()
{
bulletsinmag = magsize;
}
void FixedUpdate()
{
if (isreloading == false && isshooting == false)
{
weaponanim.CrossFade("W_Idle");
}
if (Input.GetButtonDown("r") && bulletsinmag < magsize)
{
StartCoroutine("Reload");
}
if (Input.GetButton("Fire1") && Time.time > nextFire && canfire == true && bulletsinmag > 0)
{
Fire();
}
if (bulletsinmag < 1)
{
StartCoroutine("Reload");
}
}
IEnumerator Reload()
{
if (isreloading == false)
{
canfire = false;
isreloading = true;
weaponanim.CrossFade("W_Reload");
weaponsource.clip = reloadsound;
weaponsource.Play();
if(bulletsinmag < 1)
{
StartCoroutine("DropMag");
}
yield return new WaitForSeconds(reloadtime);
{
bulletsinmag = magsize;
canfire = true;
isreloading = false;
}
yield break;
}
}
void Fire()
{
nextFire = Time.time + fireRate;
isshooting = true;
originalPos = shotcreator.transform.rotation;
RaycastHit hit;
bulletsinmag--;
weaponanim.Stop();
weaponanim.Play("W_Fire");
float randomOffset_x = Random.Range(-(0.01f - accuracy), 0.01f - accuracy);
float randomOffset_y = Random.Range(-(0.01f - accuracy), 0.01f - accuracy);
float randomOffset_z = Random.Range(-(0.01f - accuracy), 0.01f - accuracy);
Vector3 direction = shotcreator.forward;
direction.x += randomOffset_x;
direction.y += randomOffset_y;
direction.z += randomOffset_z;
shotcreator.forward = direction;
if (Physics.Raycast(shotcreator.position, shotcreator.forward, out hit, maxDist))
{
hit.transform.SendMessage("ApplyDamage", new Vector2(damage, 0),SendMessageOptions.DontRequireReceiver);
if (hit.transform.GetComponent<Rigidbody>())
{
hit.rigidbody.AddForceAtPosition(transform.forward * force, hit.point);
}
if (woodImpact && hit.transform.tag == "Wood")
{
Instantiate(woodImpact, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
else if (body && hit.transform.tag == "Body")
{
Instantiate(body, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
else if (concreteImpact && hit.transform.tag == "Concrete")
{
Instantiate(concreteImpact, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
else if (metalImpact && hit.transform.tag == "Metal")
{
Instantiate(metalImpact, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
else if (dirtImpact && hit.transform.tag == "Dirt")
{
Instantiate(dirtImpact, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
else if (vegetationImpact && hit.transform.tag == "Vegetation")
{
Instantiate(vegetationImpact, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
}
else if (deformImpact && hit.transform.tag == "Deformable")
{
Instantiate(deformImpact, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
deformer = hit.collider.GetComponent<Kerbo_Metal_Deform>();
if (deformer)
{
Vector3 point = hit.point;
point += hit.normal * forceoffset;
deformer.AddDeformingForce(point, deformingforce);
}
}
}
weaponsource.clip = shotsound;
weaponsource.Play();
shotcreator.rotation = originalPos;
isshooting = false;
}
IEnumerator DropMag()
{
yield return new WaitForSeconds(mdd);
Drop();
}
void Drop()
{
Instantiate(physmag, magspawner.position, magspawner.rotation);
}
Thanks in advance!!!!
Regards, T
There are many tutorials on object pooling, you should check those out first.
Your answer
Follow this Question
Related Questions
RayCastHit.transform.gameObject.GetComponent works in editor but not in standalone build 0 Answers
pick up item script issue 3 Answers
Unity loading prefabs difficulties. optimization 1 Answer
How to activate a script on multiple objects, but only on that object 1 Answer
in rewarded ad which code means user closed the ad by itself 2 Answers