- Home /
Why do I sometimes hit my Collider and sometimes not
Hello,
I've made a shooting system where you shoot a projectile and on this projectile is a raycast attached and as soon as the raycast hits a collider, the collider and the projectile gets destroyed.
So far so good and in fact it works in 80% of the cases but SOMETIMES the projectile just flying CLEARLY through the collider without doing anything.
I have literally no idea how to deal with the problem and for me this looks like a bug.
It would be great if anyone have an idea or even better a solution for the problem.
If needed i can post the script or images.
Thanks for help in advance :)
Answer by LeFlop2001 · Jul 01, 2020 at 09:06 PM
Why are you both using projectiles and raycasts, in the fps genre it is common to use either use raycast (simplified and less performence heavy) or a projectile (realistic), if youre using a projectile you could simply use the OnCollisionEnter() method. Regarding your problem there are a number of things that could cause this probleme and you would have to share some code to get a satisfying answer. But again i would not recommend your approach.
hello, in fact im using raycast cause of the reasons you mentioned and they recognize the gameobject and in most cases it works properly but sometimes the bullets just fly through the gameobject even tho the raycast hit the gameobject
Heres the code:
void Update()
{
countdown -= Time.deltaTime;
RaycastHit hit;
Ray landingRay = new Ray(this.transform.position, this.transform.forward);
if (Physics.Raycast(landingRay, out hit, dephei,layer$$anonymous$$ask))
{
Debug.Log(hit.collider.gameObject.name);
Collider[] collidersToDestroy = Physics.OverlapSphere(transform.position, radius);
foreach (Collider nearbyObject in collidersToDestroy)
{
EnemyStats target = nearbyObject.transform.GetComponent<EnemyStats>(); //getcomponent<Target> um auf eine funktion im anderen skript zuzugreifen
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(force, transform.position, radius);
}
Destruct dest = nearbyObject.GetComponent<Destruct>();
if (dest != null)
{
GameObject impactGO1 = Instantiate(pickupEffect, transform.position, transform.rotation);
Destroy(impactGO1, 2f);
Destroy(gameObject);
dest.Destroy();
}
if(target != null)
{
GameObject impactGO2 = Instantiate(pickupEffect, transform.position, transform.rotation);
Destroy(impactGO2, 2f);
Destroy(gameObject);
target.TakeDamage(damage);
}
}
Destroy(gameObject);
GameObject impactGO = Instantiate(pickupEffect, transform.position, transform.rotation);
Destroy(impactGO, 2f);
}
i cant tell from the script you provied how fast your projectile is supposed to be traveling but if the distance it travels every frame is greater than the width of the object youre trying to hit there is a chance it will just telereport right through it and raycast wont change that(unless you give them a big enough range), but the rigidbody has a dropdown window labeled collision detection where you can choose between discrete (which is the most performent option) and a selection of continuous options. Continuous means that its checks the traveled collision ins$$anonymous$$d of just the position for collision wich should fix your issue. if you choose to stick with your raycast id try a bigger range
Your answer
Follow this Question
Related Questions
My Raycast Won't Fire 1 Answer
scripting problem 0 Answers
Does RayCast crosses mesh colliders? 2 Answers
How to get bullets to hit crosshair 2 Answers
Finding a certain variable is failing 0 Answers