Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by K_Tec · Jul 08, 2017 at 09:47 AM · scripting problemraycastoptimization

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Jul 08, 2017 at 10:48 AM 0
Share

There are many tutorials on object pooling, you should check those out first.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges