MilkX5
March 6, 2013, 12:45am
1
I can’t figure out how to make a target block (isTrigger collider) that is attached to my character detect if it is touching an enemy(rigidbody with an attached isTrigger collider), and if so destroy it with the press of a button. I’ve tried OnTriggerStay but it doesn’t exactly work. Here’s what I have so far
function OnTriggerStay ( other : Collider ) //Rigidbody a Character Controller must be on the the target enemy for this to work
{
if (other.gameObject.tag == "enemy" && Input.GetButton("B"))
{
print ("Touching enemy");
}
if(other.gameObject.tag == "item" && Input.GetButton("B"))
{
print("I picked up an item");
}
}
Please help!
MilkX5
March 9, 2013, 1:12am
2
Nevermind I fixed my own problem after 5 days
For those who need reference, this is my final result
var player : Transform;
enum TargetTouch
{
none = 0,
item = 1,
enemy = 2
}
var targetState = TargetTouch.none;
var changeArea : boolean = false;
var canAttack : boolean = false;
var canPickup : boolean = false;
var otherObj : GameObject;
var kill : boolean = false;
function Update ()
{
var playDir = player.GetComponent("1P Attack");
kill = false;
if ( changeArea )
{
SetTarget();
}
if (canAttack)
{
if(Input.GetButtonDown("B"))
{
Destroy(otherObj);
}
}
if (canPickup)
{
if(Input.GetButton("A"))
{
Destroy(otherObj);
}
}
/*if (playDir.direction == 0) //no buttons
{
transform.position = player.transform.position;
}*/
if ( playDir.direction == 1) //north
{
transform.position.x = player.transform.position.x;
transform.position.y = player.transform.position.y + 1.1;
}
if ( playDir.direction == 2) //east
{
transform.position.y = player.transform.position.y;
transform.position.x = player.transform.position.x + 1.1;
}
if ( playDir.direction == 3) //south
{
transform.position.x = player.transform.position.x;
transform.position.y = player.transform.position.y - 1.1;
}
if ( playDir.direction == 4) //west
{
transform.position.y = player.transform.position.y;
transform.position.x = player.transform.position.x - 1.1;
}
}
function OnTriggerEnter ( other : Collider ) //Rigidbody or a Character Controller must be on the the target enemy for this to work
{
if (other.tag == "enemy" )//&& Input.GetButton("B"))
{
otherObj = other.gameObject;
changeArea = true;
print ("Touching enemy");
targetState = TargetTouch.enemy;
}
if(other.tag == "item" )//&& Input.GetButton("A"))
{
otherObj = other.gameObject;
changeArea = true;
print("I picked up an item");
targetState = TargetTouch.item;
}
}
function OnTriggerExit ( other : Collider )
{
print ("nothing");
otherObj = null;
changeArea = true;
targetState = TargetTouch.none;
changeArea = false;
}
function SetTarget ()
{
switch ( targetState )
{
case TargetTouch.none :
canAttack = false;
canPickup = false;
changeArea = false;
break;
case TargetTouch.item :
canPickup = true;
canAttack = true;
changeArea = false;
break;
case TargetTouch.enemy :
canPickup = false;
canAttack = true;
changeArea = false;
break;
}
}
function Kill ( GameObject )
{
kill = true;
}