- Home /
How do I find colliders within a cross or dot product of a vector to a target from the player?
I'm trying to find all the colliders that would fit into a cross product in front of my player so I can give them damage, but I don't know how to do that; does anybody have any suggestions?
https://www.youtube.com/watch?v=lL8xGXxEAAM
First 28 seconds.
Player Swings
Animation Event triggers detect damage for anything in the cross or dot product of the forward vector
The script passes damage value to the object health script
All Objects within the damager radius (green radian area) receive a passed event trigger (hit, stun, burn, etc., etc.)
Code So far:
void DetechCollision()
{
Collider[] hit = Physics.OverlapSphere(transform.position, radius, collisionLayer);
if (hit.Length > 0)
{
for (int i = 0; i < hit.Length; i++)
{
if (hit[i].gameObject.GetComponent<Enemy>() != null)
{
Debug.Log("Contact");
hit[i].gameObject.GetComponent<Enemy>().Hit(this.transform);
hit[i].gameObject.GetComponent<Enemy>().DealDamage(1);
}
}
if (isPlayer)
{
Vector3 hit_FX_Pos = hit[0].transform.position;
hit_FX_Pos.y += (hit[0].bounds.size.y * .5f);
Instantiate(hit_FX, hit_FX_Pos, Quaternion.identity);
}
Debug.Log("You hit " + hit[0].gameObject.name + " with " + this.gameObject.name);
gameObject.SetActive(false);
}
}
Your script is hard to understand as who/what is hitting who
Answer by andrew-lukasik · May 11, 2021 at 07:11 PM
float angleThreshold = 25f;
Vector3 myPosition = me.transform.position;
Vector3 myforward = me.transform.forward;
Vector3 otherPosition = other.transform.position;
Vector3 dirToOther = Vector3.Normalize( otherPosition - myPosition );
float dot = Vector3.Dot( myForward , dirToOther );
float angleToOther = ( 1f - (dot+1f)/2f ) * 180f;
if( angleToOther<angleThreshold )
{
// hit
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trouble with Vector3.Angle() and RotateAround() 1 Answer
How do I programmatically specify direction from a given vector? 1 Answer
Rotating a Vector3 in Instantiation 1 Answer