Hey guys, im occasionally facing an issue where my bullet sometimes misses its collision trigger and goes right through the object
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Obstacle"))
{
Bounce();
return;
}
PlayerController player = other.GetComponent<PlayerController>();
if (player != null && player.isMyTeam != isOurTeam)
{
player.OnReceiveHit(damage);
Destroy(gameObject);
}
}
private void Bounce()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit, 0.5f))
{
direction = Vector3.Reflect(direction, hit.normal);
}
}
I did put Print statements in OnTRiggerEnter to see if its actually being called, and it is, and so is Bounce() but im unsure why it just doesnt bounce sometimes. Will appreciate all help!
Thanks for the response, well 2 things, I did set the trigger mode to Continuous as well, but this cant be an issue, as I said in my first post, OnTriggerEnter is being called each time as I added debug prints inside, so the issue may be in the raycast not detecting properly
Have confirmed this, whenever the bullet passes the object its due to Ray not hitting
Can you Debug.DrawRay & maybe Debug.Break to see what’s going on? I wonder if the ray is firing from within the collider and so missing its surface (so casting from position-direction with a longer cast length might work)
edit: also i have changed it that i can manually reflect it as well, which is working too, but it would be great to solve why raycast is failing occasionally.
Your screenshot is not very clear. Is the ray intersecting the trigger? Keep in mind that a ray starting inside the collider will not hit. Try pulling the ray origin back along the velocity vector.
I dont actually think my reflecting logic is the best approach (or I have some errors). Could someone please help direct me to a more usable solution which can help bounce balls in the correct direction upon collisions
What are you trying to do? Since the physic engine need to handle every shapes, it have to let things penetrate each other, then push them out. How deep it penetrate depends on how fast the ball is moving, and its starting location relative to the collider. If it happens to penetrate deep enough, your ray origin may come inside the collider.
If the default Physics movement and bouncing is not working as you want, you can do it yourself as a ball is a simple shape with predictable trajectory:
Instead of using AddForce and such, set the position every frame yourself.
Instead of rely on collision, spherecast ahead of the ball to predict collisions and correct before it have a chance to collide.
You know the ball’s velocity, so you know where it will be in the next frame. Cast a sphere there, if it hit something, use the hit point and normal to calculate where the ball would bounce to. You can even recurse until the ray hit nothing, and will be able to have multiple bounces in a single frame.
Basically, Im trying to do something like this below
Im using the same Raycast method to detect walls to display the line renderer. Couple of things, I probably dont understand physics and rigidboys the best, but if I use OnCollisionEnter instead, I can just use the collisions normal
Vector3 normal = wall.contacts[0].normal;
direction = Vector3.Reflect(direction, normal);
But, 2 since collider isnt set to isTrigger, the bullets effect the player and apply force to it. I can set the players RB to isKinematic but then it can walk through walls etc, so I guess Rigidboy is a really weak spot in my knowledge so I do need to learn how it works, and use it properly
Those projectile don’t need to be a rigidbody. You can move them manually which give you more control and performance. You can handle collision check with a sphere cast or ray cast in fixed update.