Nice little melee system I created with triggers

I’ve been working on a 3rd person fantasy action rpg with a realtime combat system in my spare time. I had a functional melee system, but it was kind of lame because it was based on some tutorial rocket launcher scripts, where when you “fired” it would shoot a sphere collider with the mesh rendered turned off that disappeared after a few feet. I wanted it to be more based on the actual mesh and location of the sword, so I rewrote it, and this is the fruits of my labor.

(Keep in mind I’m not much of a programmer, so this was a big deal for me :))

First, I created a cylinder approximately the size of the sword i have attached to my characters hand, attached it to the sword, and disabled the renderer on it so you can’t see it in-game. Also, in order for the triggers to work properly, it needs to have a Rigidbody component attached, with “Use Gravity” disabled, and “Is Kinematic” checked. It also needs “Is Trigger” checked on its collider component. I named this object “MeleeTrigger”.

Then, I wrote this script and attached it to my MeleeTrigger, called MeleeTrigger.js:

var player : GameObject;

private var damage : float = 50.0;

function Start () {
	Physics.IgnoreCollision(collider, player.collider);
} 

function OnTriggerEnter(other : Collider){

	if(other.GetComponent(EnemyDamage)){
		other.gameObject.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
		gameObject.active = false;
	}
	
}

Then, I wrote the melee combat script to attach to the player object, that has the MeleeTrigger in his hand, called PlayerMelee.js:

var player : GameObject;
var meleeTrigger : GameObject;
var swingRate = 0.5;

function Start() {
	meleeTrigger.active = false;
	MeleeSwing();
}

function MeleeSwing(){
	while(true){
		if(Input.GetButtonUp("Fire1")){

			player.animation.CrossFade("1h_attack1");
			meleeTrigger.active = true;
			yield WaitForSeconds(player.animation["1h_attack1"].length);
			meleeTrigger.active = false;
			break;
				
			yield WaitForSeconds(swingRate);
		
		}
		else{
			yield;
		}
	}
}

Note: swingRate is an optional thing, that creates a delay in between swing, if you want to limit spamming of the attack.

So basically, whats happening is:
MeleeTrigger object is disable by default in PlayerMelee.js.
When the player presses Fire1, it plays the swing animation, enables the MeleeTrigger object for the duration of the swing, then disables it again.
Then, in MeleeTrigger.js, it checks the MeleeTrigger object collides with an object that has the EnemyDamage script attached to it, and if it does, call the ApplyDamage function, with damage as its value. The MeleeTrigger object then disables itself, so you can’t do damage more than once per swing (in the event that a crazy swing animation causes the MeleeTrigger to enter, exit, and enter the enemy’s collider again).

Woot!

1 Like

Isn’t the EnemyDamage script missing in your post?

1 Like

EnemyDamage script ?
Do you think it`s needed ?

Seems ok …so good job !
As for swingRate…
I work at a Action Tactic RPG…so i have lots of weaps.What i did was to add a Array with weaps stats and their weight .Thus i calculate the swingRate based on weaps weight and character attributes (work if you want to use same animations for eg. sword , heavy sword and axes).

Can anybody convert this to C#? I got most of it converted with zero compile errors but nothing happens when I click the fire button.

Try to mark your animation as legacy, Rightclick on the inspector tab and chang from normal to debug and change the animation type to 1 (It’s originaly 2)

I get a message saying i need a “EnemyDamage” thing. im not sure wether its a script or something. Please help.

its a script that simply has a public function called ApplyDamage which takes a float as parameter. Usually deals with the characters health by reducing their health by the float parameter aka the damage amount. Hope this helps! :slight_smile: