Making a trigger script to activate lasers once I've passed

Hello,

I am new to programming (about 2 days of tutorial experience with JavaScript) and am trying to make lasers turn on in a doorway once I’ve gone through the doorway.

I would like to use a box collider to trigger the lasers to turn on once I’ve passed the doorway.

I need someone to explain to me how to make a trigger script to make the lasers turn on ONLY once I’ve passed the door, they do not have to turn back off though.

Here is my laser script:

var height = 3.2;
var speed = 2.0;
var timingOffset = 0.0;
var laserWidth = 12.0;
var damage = 1;
var hitEffect : GameObject;

private var originalPosition : Vector3;
private var hit : RaycastHit;
private var lastHitTime = 0.0;

function Start ()
{
	originalPosition = transform.position;
	GetComponent(LineRenderer).SetPosition(1, Vector3.forward * laserWidth);
}

function Update ()
{
	var offset = (1 + Mathf.Sin(Time.time * speed + timingOffset)) * height / 2;
	transform.position = originalPosition + Vector3(0, offset, 0);

	if (Time.time > lastHitTime + 0.25  Physics.Raycast(transform.position, transform.forward, hit, laserWidth))
	{
		if (hit.collider.tag == "Player" || hit.collider.tag == "Enemy")
		{
			Instantiate(hitEffect, hit.point, Quaternion.identity);
			hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
			lastHitTime = Time.time;
		}
	}
}

@script RequireComponent (LineRenderer)

Thanks alot in advance. :slight_smile:

Cheers!
Glenn

Hi, and welcome :slight_smile:

A couple of requests:

  1. Edit your original post, and repost your code using ‘code’ tags rather than ‘quote’ tags. Also, be sure that the indentation and formatting is preserved when you post the code (you may have to convert hard tabs to spaces first to facilitate this).

  2. You didn’t actually ask a question in your post, so it’s not really clear what you’re looking for. Is your script not working? If so, in what way is it not working?

Oh, OK, sorry, fixing that now…

Cheers!
Glenn

It sounds like you’ll want to create a trigger collider and place it just past the doorway. Then, attach a script to that game object with the OnTriggerEnter() function implemented. In the body of the function, check to see if the game object that’s intersected the trigger is a game object that can trigger the lasers (if that’s necessary), and if so, instantiate the ‘lasers’ game object at the appropriate location. (You’ll probably also want to set a flag or remove the trigger game object so that the lasers are only created once.)

OK, thanks alot mate. :slight_smile:

Cheers!
Glenn