Greetings community and please help me.
I am stuck with one problem in my script. Basically script should make platform fall after 3 seconds as soon as it collided with a specified game object.
Here is my script:
var fallTime: float = 3.0; //Interval to fall
private var fall: float;//Time to fall
private var canFall: boolean;
var object: GameObject;
private var rb: Rigidbody;//Object rigidbody;
function Start(){
canFall = false;
fall = 0.0;
}
function OnTriggerEnter(col: Collider){
if(col.gameObject.tag == "EarthSpirit"){
Debug.Log("PlatformWillFall");
canFall = true;
rb = object.gameObject.AddComponent(Rigidbody);
rb.useGravity = false;
}
}
function Update(){
Debug.Log(Time.time);
if(canFall Time.time > fall){
fall = Time.time + fallTime;
rb.useGravity = true;
}
}
The problem is that as soon as game object collides with a platform, it falls down with no delay. I think problem is in Time.time, because it is calculating from the moment scene starts.
Thanks for any help in advance.
ok, you need a “start” time when the collision happens, and the update should be checking for TimeNow > StartTime + Delay
so somewhere in the OnTriggerEnter function you need something like
startTime = Time.time;
and the if in the Update function should look something like
if(Time.time > startTime + delay)
{
...
Hmm… Tried your solution and it is still not working…
startTime = Time.time still calculates time no matter what ( and yeah i inserted it in OnTriggerEnter function ). I logged its value and it shows that as soon as game object reaches trigger and collision occurs, value of startTime variable already over 3 seconds.
Any ideas?
yes, Time.time starts when you hit play…
Time.time at start 0
collision happens at Time.time = 10 → store the Time.time into StartTime var
in each update, check if Time.time > StartTime (ie 10) + Delay (ie 3)
when Time.time > 13 the contents of the if block happens.
actually there’s an example of a delay on Time.time reference page: Unity - Scripting API: Time.time
Oh yeah, that’s right… Thanks my friend