Hello,
I’m using the below scripts to create and destroy an object that can be carried by a player using the dragrigidbody script from the standard assets package.
It’s set up so that when you’re holding the object a timer IS NOT running and if you let go of the object a timer starts.
At certain times the object can be placed inside a trigger box to activate functions in another script. I’ve added a layer mask to the dragrigidbodyscript so that the raycast can ‘penetrate’ the trigger boxes when you grab the object.
Here’s the problem: Sometimes the object timer continues to run after you’ve grabbed the object if it was grabbed inside the trigger box. Can any see why that might be happening?
//Rock_Monitor.js
//attached to the object I want to grab
public var totalTime : float = 5.0; // Use this to set timer duration
private var timer : float; // Actual timer
private var runTimer : boolean = false; // Run/pause timer
var popRock : AudioClip;
function Awake () {
timerReset();
}
function OnMouseDown ()
{
print ("Holding Rock");
timerReset();
runTimer = false;
}
function OnMouseUp ()
{
print ("Released Rock");
timerEnabled();
}
function Update() {
if(runTimer) timer -= Time.deltaTime;
if(timer <= 0.0) selfDestruct();
}
public function setTotalTime(value : float) {
totalTime = value;
}
public function timerEnabled() {
print ("timer running");
runTimer = true;
}
public function timerReset() {
timer = totalTime;
}
public function selfDestruct() {
// Play sound, particle effect, notify player, etc.
if (popRock)
{
AudioSource.PlayClipAtPoint(popRock, transform.position);
}
allScripts = FindObjectsOfType(RockRespawner);
for (var script : RockRespawner in allScripts)
{
script.RockDeadTrigger();
}
print ("Rock Destroyed");
Destroy(gameObject);
}
and then this is attached to dispensers that you can collect the new objects from if the old one dies.
//RockRespawner.js
var NewRock : Rigidbody;
private var RockDead : boolean = false;
function RockDeadTrigger()
{
print ("RockDead Message Sent");
RockDead = true;
}
function OnTriggerEnter (other : Collider)
{
if (other.tag == "Player" RockDead){
print("New Rock Created");
yield WaitForSeconds (1);
Instantiate (NewRock, transform.position, transform.rotation);
RockDead = false;
}
}
in the trigger boxes that seem to be causing the problems i just have various scripts that look like:
function OnTriggerEnter (other : Collider)
{
if (other.tag == "Rock"){
print("rock is on the button");
GameObject.Find("SlidingCube1").GetComponent("slider").SendMessage("doorButtonUp");
}
}
function OnTriggerExit (other: Collider)
{
if (other.tag == "Rock"){
print("rock is off the button");
GameObject.Find("SlidingCube1").GetComponent("slider").SendMessage("doorButtonDown");
}
}
Any ideas anyone?