Hi all, I have two triggers( i’m trying to make a fighting game) , but the standard OnTriggerExit and OnTriggerEnter
isn’t working .
Any idea on how to get this to work .
//
var OtherTag : String ;
function OnTriggerExit (other : Collider) {
//if(other.tag == OtherTag){
if (other.CompareTag (OtherTag))
{
CanPunch = false ;
}
// }
// Destroy(other.gameObject);
}
function OnTriggerStay (other : Collider) {
if (other.CompareTag (OtherTag)) {
CanPunch = true;
}
// Destroy(other.gameObject);
}
If you mean, each player has a trigger on his fist, for example, why not either add a collider which is not a trigger, to each fist. Or, just use non trigger colliders, or set trigger to true if punching. Set it to false if not punching.
You need a Rigidbody or a CharacterController to trigger triggers. Colliders on their own are passive.
I ended up adding ridgbody’s to both fighters . I did some other things too … much convoluted code that will bite me in the backside latter
Anyway, for anyone else who needs it , here’s the full script
#pragma strict
var Punched : boolean ;
var InPunch : boolean ;
var OtherTag : String ;
var CanPunch : boolean ;
var SendBack : Listen ;
var HP : float ;
var HPcommand : ChecknSend ;
var TakeHit : boolean ;
// no reason to not just assign this
var OtherFighter : Fight ;
var Controled : boolean = true;
var AIcommand : AIone ;
var OtherGuy : GameObject ;
var AIhits: boolean ;
InvokeRepeating("AIpunch",1,1);
public function TakingHit() {
if(TakeHit!= true){
TakeHit = true ;
SendBack.React();
HP--;
yield WaitForSeconds(0.5);
TakeHit = false;
}
}
function Start () {
}
/*
function OnCollisionStay(collision : Collision) {
if(collision.gameObject == OtherGuy)
{
CanPunch = true;
}
}
*/
///*
function OnTriggerExit (other : Collider) {
//if(other.tag == OtherTag){
if (other.gameObject.CompareTag (OtherTag))
{
CanPunch = false ;
if(!Controled){
AIhits = false;
}
}
// }
// Destroy(other.gameObject);
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag (OtherTag)) {
CanPunch = true;
if(!Controled)
{
AIhits = true ;
}
}
// Destroy(other.gameObject);
}
//*/
public function Hit () {
//if(!Punched){
if(CanPunch == true){
Punched = true ;
OtherFighter.TakingHit();
yield WaitForSeconds(0.3);
Punched = false;
//}
}
}
public function PunchinSec() {
InPunch = true ;
yield WaitForSeconds(0.1);
InPunch = false;
}
public function AIpunch()
{
if(!Controled){
if(AIhits)
{// the ai takes punches he can land
if(CanPunch == true){
/// this SHOULD ONLY be used in AI cases
SendBack.PunchOut();
Hit();
}
}
}
}
function Update () {
if(Controled){
HPcommand.LocHPA = HP ;
}
if(!Controled){
HPcommand.LocHPB = HP;
}
if(Input.GetButtonDown("Punch"))
{
if(Controled){
SendBack.PunchOut();
Hit();
}
}
}