x


Simple collision script not working, what am I doing wrong?

Hello,

I'm pretty new to this scripting stuff. In my scene, I have a character in a room. He opens the door by stepping on a switch. This leads to a second room. In this room, you pick up an item to open another door. The scripting for these 2 events works just fine. The problem happens when I try to use the same type of script to make a third door open when you shoot a target. I can shoot the target and get the door to slide open, but when I shoot the target again, the door keeps sliding.

In my first room, I can step on the switch to open the door, and repeatedly stepping on the switch does nothing (as intended), but using the same kind of script to shoot a target with a fireball for the third room just doesn't seem to end correctly. The door will move every time I shoot the target.

Here is the first script:

var door1opened : boolean = false;
var door1opening : boolean = false;
var timer : float = 0.0;
var door2opened : boolean = false;
var door2opening : boolean = false;
static var Fireballready : boolean = false;

function OnControllerColliderHit(hit:ControllerColliderHit) {

if((hit.gameObject.tag == "Switch1") && (door1opened == false))
{
	door1opening = true;
}

if(hit.gameObject.tag == "Fireball Item")
{
	Destroy(hit.gameObject);
	door2opening = true;
	Fireballready = true;
}

}

function Update () {

if((door1opening) && (door1opened == false))
{
	var door1 = GameObject.FindWithTag("Door1");
	door1.transform.Translate(0, 0, -2 * Time.deltaTime);
	timer += Time.deltaTime;

	if((timer >= .5) && (timer < 1.5))
	{
		door1 = GameObject.FindWithTag("Door1");
		door1.transform.Translate(2 * Time.deltaTime, 0, 2*Time.deltaTime);
	}

	if(timer >=1.5)
	{
	        door1opening = false;
		door1opened = true;
		timer = 0.0;
	}
    }

if((door2opening) && (door2opened == false))
{
	var door2 = GameObject.FindWithTag("Door2");
	door2.transform.Translate(0, 0, -2 * Time.deltaTime);
	timer += Time.deltaTime;

        if((timer >= .5) && (timer < 1.5))
	{
	    door2 = GameObject.FindWithTag("Door2");
	    door2.transform.Translate(2 * Time.deltaTime, 0, 2 * Time.deltaTime);
	}

	if(timer >=1.5)
	{
	    door2opening = false;
	    door2opened = true;
	    timer = 0.0;
	}
}

}

This first script is attached to the player object. It controls the first 2 doors as well as unlocking the ability to shoot a fireball. This all works just fine.

The second script is where the problems are (I think):

var door3opened : boolean = false;
var door3opening : boolean = false;
var timer : float = 0.0;

function OnCollisionEnter(collision : Collision) {

if((collision.gameObject.tag == "Target1") && (door3opened == false))
{
    door3opening = true;
}

}

function Update () {

if((door3opening) && (door3opened == false))
{
	var door3 = GameObject.FindWithTag("Door3");
	door3.transform.Translate(0, 0, -2 * Time.deltaTime);
	timer += Time.deltaTime;

	if((timer >= .5) && (timer < 1.5))
	{
	    door3 = GameObject.FindWithTag("Door3");
	    door3.transform.Translate(2 * Time.deltaTime, 0, 2 * Time.deltaTime);
	}

	if(timer >=1.5)
	{
		door3opening = false;
		door3opened = true;
		timer = 0.0;
	}
}

}

This second script is attached to my fireball prefab that the character shoots.

Apologies if this looks sloppy. I'm learning.

So, the problem to me seems that OnControllerColliderHit from the first script works differently than OnCollisionEnter from the second script. Other than that I'm at a total loss. Both objects have colliders and rigid bodies.

Thanx for your time.

more ▼

asked Nov 30 '09 at 01:36 AM

Grim420 gravatar image

Grim420
30 4 4 8

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

The reason is that the last script (for door3 opening) is attached to the fireball, so with every fireball fired you get a new instance, thus door3opened = false, as the new fireball hasn't opened a door yet. Solution would be to change the initialisation to:

global var door3opened : boolean = false;
var door3opening : boolean = false;
var timer : float = 0.0;

If you get more into javascript it may be worth to restructure your solution, for example have the door opening code attached to the door instead of the fireball. A coroutine also would clean up the code nicely (that would get rid of an update, not really a problem if you're on pc/mac platform, but if you're developing for iphone you want to get rid of as many updates as possible :-).

An explanation of global var versus var can be found at the forums.

more ▼

answered Dec 01 '09 at 12:14 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left

Hello I am new in unity i am a 3d guy, not the scripter, already testign the topic of the collisions, and looking your script superficially, i can see:

GameObject.FindWithTag is bad write is a : gameObject.FindWithTag change the "G" for the "g"

Also have carefull that "function OnControllerColliderHit" is only for use on character controller.

use more the "debug" for show on console if your "hits" works, is a very helper, example: Debug.Log ("object1 hit");

sorry for my bad english.

more ▼

answered Nov 30 '09 at 02:38 AM

pope gravatar image

pope
12 1

Thanks for the response. I fixed the "G" problem and ran some debug. A collision is definitely taking place, its just not turning the door3openeing variable true like its supposed to. Its somehow still running the stuff in the Update funtion without switching any variables or running the timer.

Nov 30 '09 at 03:00 AM Grim420
(comments are locked)
10|3000 characters needed characters left

Thank you very kindly.

more ▼

answered Dec 01 '09 at 02:15 AM

Grim420 gravatar image

Grim420
30 4 4 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5087
x2499

asked: Nov 30 '09 at 01:36 AM

Seen: 6487 times

Last Updated: Nov 30 '09 at 02:57 AM