3rd person melee - Trigger Event is not working well

Hey, Guys

Guess what? I’m having some problems here.

So, I’m making a 3rd person action game, and I’m using colliders like triggers to check if I’ve been hit by enemy’s sword, or I’ve hit the enemy with my sword. But that’s strange, because, sometimes works, sometimes it doesn’t. Well, I’ll show the scripts so far:

The Enemy’s AI:

var waypoint : Transform[ ]; // The amount of Waypoint you want

var patrolSpeed : float = 3; // The walking speed between Waypoints

var loop : boolean = true; // Do you want to keep repeating the Waypoints

var player : Transform; // Referance to the Player

var dampingLook = 6.0; // How slowly to turn

var pauseDuration : float = 0; // How long to pause at a Waypoint

var attackRange = 10; // Range to start the attack

var attackSpeed = 5.0; // Speed to attack

var attackLook = 10.0; // How fast to turn when attacking

var AtkSpeed = 1;

var PlayerStat : GameObject;

var energy : int = 1;

var GameStat : GameObject;

private var YouDead : boolean = false;

private var distanceToPlayer : int; // Distance from the enemy to the Player

private var curTime : float;

private var currentWaypoint : int = 0;

private var character : CharacterController;

private var gravity : float = 2.0;

private var attacking : boolean = false;

private var isattacking : boolean = false;

function Awake(){

}

function Start(){

// collider.isTrigger = false;

Physics.IgnoreLayerCollision(9,10,true);

// var GameStat : GameObject;

GameStat = GameObject.Find(“GameDados”);

PlayerStat = GameObject.Find(“ninja”);

character = GetComponent(CharacterController);

animation[“attack”].speed = AtkSpeed;

animation[“attack”].layer = 1;

animation[“hit”].layer = 2;

animation[“dead”].layer = 3;

animation[“hit”].speed = 3;

}

function OnTriggerExit (other : Collider) {

if(other.gameObject.tag == “playerattack”){

animation.Stop(“attack”);

animation.Play(“hit”);

if (animation.IsPlaying(“hit”))

{

energy -= 1;

}

}

}

function Update(){

if (energy <= 0) {

// collider.isTrigger = false;

GameStat.GetComponent(GameStatus).Score += 500;

patrolSpeed = 0;

attackSpeed = 0;

attackLook = 0;

animation.Play(“dead”);

GetComponent(IA).enabled = false;

//character.enabled = false;

//attacking = false;

}

if (PlayerStat.GetComponent(PlayerStatus).died) {

attacking = false;

animation.Stop(“attack”);

}

distanceToPlayer = Vector3.Distance(player.position, transform.position); // Distance between the enemy and the Player

if(distanceToPlayer < attackRange){

attacking = true;

attack();

}else{

attacking = false;

}

if(currentWaypoint < waypoint.length !attacking){

patrol();

}else{

if(loop !attacking){

currentWaypoint=0;

}

}

}

function patrol(){

var target : Vector3 = waypoint[currentWaypoint].position;

target.y = transform.position.y; // Keep waypoint at character’s height

var moveDirection : Vector3 = target - transform.position;

if(moveDirection.magnitude < 0.5){

if (curTime == 0) {

curTime = Time.time; // Pause over the Waypoint

animation.Play(“idle”);

}

if ((Time.time - curTime) >= pauseDuration){

currentWaypoint++;

curTime = 0;

}

}else{

// Look at and dampen the rotation

var rotation = Quaternion.LookRotation(target - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);

character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);

animation.Play(“walk”);

}

}

function attack(){

// Attack the Player

if (distanceToPlayer <= 1.5) {

animation.Play(“attack”);

}

else {

animation.Stop(“attack”);

}

// Rotate to face the Player

var lookPos = player.position - transform.position;

lookPos.y = 0;

var rotation = Quaternion.LookRotation(lookPos);

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);

// Move towards the Player

var target : Vector3 = player.position;

var moveDirection : Vector3 = target - transform.position;

moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis

moveDirection.y -= gravity; // Add gravity so the he always stays on the ground

character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);

animation.Play(“run”);

}

The script that checks if the player has been hit:

public var PlayerLife = 3;
//var PlayerLives = 3;
var player : Transform;
private var m_isHit : boolean = false;
var died : boolean = false;
var GameStat : GameObject;

private var curTime : float;
var deadDuration : float = 0;
//function Awake () {
// DontDestroyOnLoad (transform.gameObject);
//}

function Start () {
animation[“hit”].layer = 1;
animation[“dead”].layer = 2;
animation[“dying”].layer = 3;

animation[“hit”].speed = 3;

GameStat = GameObject.Find(“GameDados”);

}

//function OnControllerColliderHit(hit: ControllerColliderHit){

function OnTriggerExit (other : Collider) {
if(other.gameObject.tag == “attackPoint”){
animation.Play(“hit”);
if (animation.IsPlaying(“hit”))
{
PlayerLife -= 1;

}

}
}

function Update () {

print (“Health :” +PlayerLife+ " - Lives :" +GameStat.GetComponent(GameStatus).PlayerLives+ " Score :" +GameStat.GetComponent(GameStatus).Score);
//print (“Lives :” +PlayerLives);

if (PlayerLife <= 0 !died) {
GetComponent(ThirdPersonController).enabled = false;
GameStat.GetComponent(GameStatus).PlayerLives --;
animation.Play(“dying”);
animation.PlayQueued(“dead”, QueueMode.PlayNow);
died = true;

}

if (!animation.IsPlaying(“dying”) died) {

//animation.CrossFade(“dead”);

Application.LoadLevel (1);

}

}
//function Die() {

//animation.Stop(“dead”);
//PlayerLife = 3;
//died = false;
//}

And then… I’m clueless =/

And sorry for my English, I’m from Brazil

P.S: I’m suppose to post that in the Unity Answers, but the page is off

there’s quite a lot out there for melee combat… i believe these are a good line of tuts… burgzergarcade.com
I’m not really a programmer, but just try to help you in anyway…