Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 saltyshores · Feb 16 at 02:37 AM · triggerfpsprojectile

How to speed up a trigger enter function?

I am making a FPS game and the trigger rate doesn't run quick enough to hit all objects, here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CustomNormalBullet : MonoBehaviour { public float Damage = 25f; public float lifetime = 3f; public float afterImpactLife = 0.5f;

 private void Awake()
 {
     StartCoroutine(DelayedDestroy());
 }

 private void OnTriggerEnter(Collider other)
 {
     Debug.Log("hit " + other.name + "!");
     Target target = other.GetComponent<Target>();
     if (target != null) target.TakeDamage(Damage);  
 }

 IEnumerator ImpactDelayDestroy()
 {
     yield return new WaitForSeconds(afterImpactLife);
     Destroy(gameObject);
 }

 IEnumerator DelayedDestroy()
 {
     yield return new WaitForSeconds(lifetime);
     Destroy(gameObject);
 }

} I have a rigidbody system to shoot the bullet at "200 force" in rigidbody terms, I have calculated a 70 percent error rate, is there any way to increase the frequency of the trigger scanning?

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 xxmariofer · Feb 16 at 07:18 AM 0
Share

there are 2 ways, changing the rigidbody settings to continues dinamic or increasing the fixed time step (not recommended, will recalculate ALL forces more frequently, not just triggers)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Captain_Pineapple · Feb 16 at 12:07 PM

Hey,

you can either follow @xxmariofer s advice which would probably work in most cases.

BUT

it doesn't have to. If you have small fast moving objects in Unity then the actual impact calculations can be weird to say the least.


The most reliable way to get a proper small projectile would be a raycast approach. Now i don't mean hitscan but basically a custom projectile with a body that moves to a certain point and does a raycast to its movement direction each frame. If something is hit, hit it. If nothign is hit, move there and wait for the next frame. You'd have to apply gravity artifically but this system works quite good for me in general and solved all my issues that i formally had with missing/inaccurate hits.

Comment
Add comment · Show 1 · Share
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 saltyshores · Feb 16 at 01:36 PM 0
Share

Thank you so much my projectile gun now works without problems!

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

196 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 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

Showing text near object 1 Answer

Rotate RayCast/Object right on instantiation? 1 Answer

Projectile not inflicting damage on AI 1 Answer

Don't allow the player to shoot while projectile is travelling 1 Answer

making the weapon shoot whatever the crosshair is on 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