Hi everyone, in my game i want the layer to climb the stairs.Each step can hold him for seconds before collapse . The code worked just fine with one step but when i ayyach it to multiple steps and the player collide with the first one all the steps collapse at the same time.
**//the time that one step can hold the player
var stillTime=2.0;
var hitEnter:float;
function OnCollisionEnter()
{
hitEnter=Time.time;
}
function Update()
{
if(Time.time>(hitEnter+stillTime))
{
Destroy(gameObject);
print("i can't support you any longer :p");
}
}**
Do i have to attach a different code sheet to every step ??
2 Answers
2
Your problem is that if you don't collide with the platform your hitEnter time will be "0". Therefore all platforms will collapse after two seconds.
Do something like:
var stillTime = 2.0;
var hitEnter = 0.0;
function OnCollisionEnter()
{
if (hitEnter == 0.0)
{
hitEnter = Time.time;
}
}
function Update()
{
if((hitEnter > 0.0) && (Time.time>(hitEnter+stillTime)))
{
Destroy(gameObject);
print("i can't support you any longer :p");
}
}
That will only allow a platform to collapse when you have set a time. It also prevents the "retriggering" of the time when touching the platform again.
The code work fine when i use triggers
function OnTriggerEnter()
{ yield WaitForSeconds(2);
Destroy(gameObject);
}
i attached two collider to each step one as trigger and the other for colliding thank you for helping me
From the code you have here you'll need to attach the script to each step.
– Toxic_BlobThe problem is not in attaching the code to each step but in duplicating the code sheet for each step step1=> code1 step2=> code 2 ...
– anon69321326