OnTriggerEnter works...sometimes

发布时间:2026-01-18 23:34

科技教育工具十六:科学实验app如How It Works解释科学原理 #生活知识# #科技生活# #科技教育工具#

I was working on a small game in which you shoot projectiles towards various targets, now I decide to use OnTriggerEnter instead of OnCollisionEnter because OnCollisionEnter adds a slight force to the target during collision and I don't want that(btw, if anyone knows how to fix that, it'd be great), so I had to use OnTriggerEnter instead.

So the problem is that my code works just fine when the target is still or static. But when the target is in motion, sometimes the projectile works just fine as wanted(damages the target and disables itself afterwards) and other times, the projectile literally just moves through the target without causing any damage.

I say "sometimes" because this happens completely randomly.

Here's is my code:

public int m_DamageAmount; public int speed; private Rigidbody m_Rigidbody; private void Awake() { m_Rigidbody = GetComponent<Rigidbody>(); } private void FixedUpdate() { m_Rigidbody.AddForce(transform.forward * speed, ForceMode.Impulse); } private void OnTriggerEnter(Collider other) { if (other.gameObject.layer == LayerMask.NameToLayer("Enemy")) { ObjectHolders._ImpactFX.Play(); ObjectHolders._ImpactFX.transform.position = other.transform.position; EnemyBrain enemy = other.GetComponent<EnemyBrain>(); if (enemy != null) enemy.Damage(m_DamageAmount); gameObject.SetActive(false); } }

Here are other things I've tried:

1) I have changed both the target and the projectile's movements from transform to rigidbody.

2) I have tried changing my game time's Fixed Timestep value from 0.02 to 0.1.

These things did not have any visual effects on it.

网址:OnTriggerEnter works...sometimes https://c.klqsh.com/news/view/321208

相关内容

Understanding Free Cultural Works
how to watch foxtel overseas?
Cast of How I Met Your Mother
英语口语流行话题 第29期:动漫迷 15
2. Copyright Law
关于爱好的英语短文阅读
What are the purposes of the ARM ABI and EABI?
How to play Bing Homepage Quiz and win?
Water Leak Repair and Detection Service – ARS/Rescue Rooter
Portrait of a Man by Jan Van Eyck

随便看看