- Home /
[Solved] Character Controller Explosion Detection
So, i'm trying to create a system where you can find out if you are going to take damage from an explosion if you are behind cover. I plan to shoot ray-cast to 8 positions from the rockets detonation position to the players edges (character controller) and if the player is hit by the ray-cast then that clarifies that the player should take damage. However, when the position to test for is not equal to the center of the character controller (collided.bounds.center) sometimes it doesn't detect the collision. Like in the image shown all points hit the collider at the same spot but not all return that any damage was taken. At some rotations the collision works while others they don't. Currently i'm only checking for one raycast since its not working 100% of the time. Any clues why there is the rotation issue? Thanks.
private void Explode()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, radius, mask);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject.tag == "Player") //If there is a player inside the rocket sphere
{
int baseDamage = CoverCheck(colliders[i]);
Debug.Log(colliders[i].gameObject.name + " took " + (baseDamage * damage) + " damage");
}
}
Destroy(this.gameObject,3);
}
private void OnDrawGizmos() //Draw the area of potential rocket damage
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, radius);
}
private int CoverCheck(Collider collider) //See how much of the character is covered if any
{
Vector3 v3Pos = transform.position;
Bounds bounds = collider.bounds;
int notBlocked = 0;
Vector3 v3Corner = Vector3.zero;
Vector3 v3Center = bounds.center;
RaycastHit hit;
//Sets a new raycast destination to check for
v3Corner.Set(v3Center.x, v3Center.y + 1.5f, v3Center.z + 0.6f);
Debug.DrawLine(v3Pos, v3Corner, Color.green, 20.0f);
if (Physics.Linecast(v3Pos, v3Corner, out hit) && hit.collider.name == "Player")
{
notBlocked++;
}
return notBlocked;
}
Answer by Jadlar · Aug 01, 2019 at 03:33 AM
Solution: the CharacterController has some kind of issues with Raycast I have not looked into the reasons exactly why, but that is the fundamental issue. In my case since I still need my CharacterController all I did is add a capsule collider slightly above the CharacetrController collider. With this all Raycast can be detected along the outside of the collider which is what i'm looking for.
Your answer
Follow this Question
Related Questions
Detect RaycastHit on Character Controller 2 Answers
How can i detect the normal of the collider the Character Controller is touching. 2 Answers
Help Understanding Raycast 2 Answers
How do I make a Character Controller NOT collide with Rigidbodies? 3 Answers
GameObjects glitching through other Objects when being held 0 Answers