# Restricting my horizontal slash attack

I've been working on making this slash attack for my character in the unity engine. The whole point to this script is to put the damage range in sync with the slashing animation. The slash is completely horizontal and is scripted out make enemies bounce back. I have most of that but what I really need to restrict it on x axis. If I haven't explained it enough let me give you an example of what its doing. When an enemies in front of me but above the area my swing attack shouldn't be able to reach. I use my swing attack and it still does damage anyways. what I need is a way is restrict my hit detection range between two angles rolling around the x axis. I'm sorry if I didn't explain this so well just tell me if I need to make something more clear.

this is the code I've come up with what I've got it doings is it applies damage it can tell if an enemy is in front of it (uses the dot product for that) it can even knock said enemy backwards

void Knockback(){

Vector3 grenadeOrigin = transform.position; // I'm building this controller using explode for knock back
// and this stuff was based off of a grenade script
foreach (Collider hit in colliders){
Rigidbody rb = hit.GetComponent<Rigidbody>();
if (rb){
Vector3 dir = (rb.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if (direction >0.1){// this is where i tell if something is in front of me
//This is where we apply force to our enemy and various other rigid bodies
//This is where we apply damage to the enemy
}
}
}
}


My solution to this problem would be to simply use OnColliderEnter:

private void OnColliderEnter(Collision collision)
{
}


This problem solution assumes that the slash execution is accomplished with an animated GameObject.

And also I'd advise you to access MonoBehaviours using GetComponent because of better performance and readability:

collision.gameObject.GetComponent<YourDamageScript>().Damager(damage);

• I'm actually using on trigger enter is there a way to use AddExplosionForce with on trigger enter? – Ryan Henry Jul 1 '15 at 23:17
• These two dont affect eachother. Thats no difference, but beware, triggers allow other colliders to pass through them. – Doctor Niklas Jul 2 '15 at 5:20
• well the code i have set up for triggers does not seem to recognize the reference AddExplosionForce which should just have to deal with the rigidbody I wonder if this has to do with triggers Collider variable and OnColliderEnter's Collision variable – Ryan Henry Jul 6 '15 at 22:36
• The weapon collider just has to have "isTrigger" set to true. I dont really understand the rest of your comment. – Doctor Niklas Jul 7 '15 at 5:06
• I was refering to the variables withing the parameter because OnColliderEnter uses Collision which is the unity defined variable that only works with the functions that do not use triggers. OnTriggerEnter uses Collision which only works for the function OnTriggerEnter. – Ryan Henry Jul 7 '15 at 15:22