getting trigger to only recognise player

Greetings all…

So, I have a door open script which I found on here and changed a bit as follows :-

var targeti : Transform;
var playeri : Transform;
var moveSpeed : float = 5.0;
var upMaxDistance : float = 10.0;
var openCloseSound : AudioClip;
private var initPosition : Vector3;
private var openDoor : boolean = false;

function Start() {
	initPosition = targeti.transform.position;	
}

function Update () {
	if (!targeti) {
		return;	
	}
	if (!playeri) {
		return;	
	}
	if (openDoor == true) {
		targeti.position.y = Mathf.Min(upMaxDistance, targeti.position.y+moveSpeed * Time.deltaTime);
	}
	else {
		targeti.position.y = Mathf.Max(initPosition.y, targeti.position.y-moveSpeed * Time.deltaTime);	
	}
}

function OnTriggerEnter() {
	openDoor = true;	
	audio.PlayOneShot(openCloseSound);
}

function OnTriggerExit() {
	yield WaitForSeconds(2);
	openDoor = false;	
	audio.PlayOneShot(openCloseSound);
}

Using this I can define the target of the trigger and the object which triggers it… player or monster etc.

However… when I set the trigger to be my player char… the door works perfectly, but so does a rocket fired by the player…

Is this because the rocket is a child of the player and so is seen as “the player” as well?

Interestingly, if opened by a rocket, it doesn’t close again as I assume that as the rocket is destroyed, it never leaves the trigger to trigger the exit routine.

If so, or if I am completely wrong, how would I go about getting only my player to trigger the door please? Got me baffled this one :slight_smile:

Regards

Graham

You must identify the player in the Trigger function:

function OnTriggerEnter (other:Collider) {
if (other.gameObject.CompareTag (“Player”)) {
openDoor=true;
audio.PlayOneShot(openCloseSound);
}
}

And the same in OnTriggerExit function:

function OnTriggerExit (other:Collider) {
if (other.gameObject.CompareTag (“Player”)) {
yield WaitForSeconds(2);
openDoor=false;
audio.PlayOneShot(openCloseSound);
}
}

Many thanks angel_m

That solved it and I got it now… Must have been having a senior moment.

Regards

Graham