Hey guys,
I’m very new to Unity, and I’m running into some trouble right now. Here’s my situation:
I have enemies that have a Box Collider on them, and a larger Sphere Collider attached as a trigger. I also have a code that makes the enemy chase the player when they Enter the Sphere Collider (trigger). Here’s that code.
#pragma strict
//This script makes the enemy chase the player if they are close enough
// and change color to show if they are chasing or not.
var enemyTarget : Transform;
var shouldLook : boolean = false;
var speed : int = 2;
var enterGrowl : AudioClip;
var exitGrowl : AudioClip;
function Start ()
{
this.renderer.material.color = Color.blue;
animation.CrossFade("idle");
}
function Update ()
{
//Look at target or not and move towards it or not
if (shouldLook == true)
{
this.transform.LookAt(enemyTarget);
rigidbody.velocity = transform.forward * speed;
}
}
function OnTriggerEnter(other: Collider)
{
Debug.Log("TRIGGERED BY " + other.name);
//if the thing that entered my trigger is the player...
if (other.tag == "Player")
{
enemyTarget = other.gameObject.transform;
shouldLook = true;
//make em look angry
this.renderer.material.color = Color.red;
animation.CrossFade("run");
audio.PlayOneShot(enterGrowl);
}
}
function OnTriggerExit(other : Collider)
{
if (other.tag == "Player")
{
shouldLook = false;
//make em look peaceful
this.renderer.material.color = Color.blue;
animation.CrossFade("idle");
audio.PlayOneShot(exitGrowl);
}
}
That works fine. The problem is, I have a script attached to my sword that is supposed to kill the enemies when it hits the Box Collider. However, if you have the sword activated when you enter the sphere collider, the enemy is destroyed. Here’s the code for the sword:
#pragma strict
var boolTest : boolean;
static var attackMelee : boolean = true;
function Start ()
{
renderer.enabled = false;
}
/*
function OnTriggerEnter (other : Collider)
{
Destroy(other.gameObject);
}
*/
function OnTriggerEnter(other: Collider)
{
// Debug.Log ("Hello");
if (other.tag == "E1" && renderer.enabled == true)
{
Destroy(other.gameObject);
audio.PlayOneShot(clang);
// Debug.Log ("DIE BOX!");
}
}
var swordSFX : AudioClip;
var clang : AudioClip;
function Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
audio.PlayOneShot(swordSFX);
}
if (Input.GetKey("mouse 0"))
{
renderer.enabled = true;
}
else {renderer.enabled = false;}
}
I want to fix it so that the sword will only destroy the enemies if it hits the box collider, but not the sphere collider which is triggering the enemies and making them chase the player.
I’m very new to Unity, and I was given these codes in a class, so I’m just trying to make them work.
Thanks guys