0
\$\begingroup\$

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 
     Collider[] colliders = Physics.OverlapSphere (grenadeOrigin, radius);
     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
                 rb.AddExplosionForce(power, grenadeOrigin, radius, 0);
                 //This is where we apply force to our enemy and various other rigid bodies 
                 hit.gameObject.SendMessage("Damager", Damage, SendMessageOptions.DontRequireReceiver);
                 //This is where we apply damage to the enemy
             }
         }
     }
 }
\$\endgroup\$
0
\$\begingroup\$

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

private void OnColliderEnter(Collision collision)
{
    collision.rigidbody.AddExplosionForce(power, transform.position, radius, 0);
    collision.gameObject.SendMessage("Damager", Damage, SendMessageOptions.DontRequireReceiver);
}

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);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I'm actually using on trigger enter is there a way to use AddExplosionForce with on trigger enter? \$\endgroup\$ – Ryan Henry Jul 1 '15 at 23:17
  • \$\begingroup\$ These two dont affect eachother. Thats no difference, but beware, triggers allow other colliders to pass through them. \$\endgroup\$ – Doctor Niklas Jul 2 '15 at 5:20
  • \$\begingroup\$ 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 \$\endgroup\$ – Ryan Henry Jul 6 '15 at 22:36
  • \$\begingroup\$ The weapon collider just has to have "isTrigger" set to true. I dont really understand the rest of your comment. \$\endgroup\$ – Doctor Niklas Jul 7 '15 at 5:06
  • \$\begingroup\$ 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. \$\endgroup\$ – Ryan Henry Jul 7 '15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.