In the platform game that I’m creating I have what I’m calling a speed pill which will temporarily boost the speed of the character I have for 5 seconds
What I need to do is on the speed pill item apply a script with an OnTriggerEnter function that adjusts the speed variable in the script called PlayerController to 20 for 5 seconds then it returns to 10. My problem is that I don’t know how to do that, as I don’t know how to refer to a variable in another script as my programming skills aren’t so good.
So far I have the following -
function OnTriggerEnter(otherObj: Collider)
{
if (otherObj.tag == "Player")
{
//here needs to be a function that adjusts the speed
//variable in PlayerController from 10 to 20 for 5 seconds
}
}
Coroutines allow you to execute a bit of code and then yield (wait) for an amount of time before continuing execution.
I am not so familiar with unityscript, so this might not compile but it would look something like this
function SpeedUp(float time)
{
var player = otherObj.GetComponent.<PlayerController>();
player.speed = 20;
yield return new WaitForSeconds(time);
player.speed = 10;
}
Note that if you were to hit a second speed pill before the 5 seconds was up it would not further increase your speed. This may or may not be what you want to happen in this situation.
function Speed(go:GameObject){
go.GetComponent(PlayerController).speed = 20;
yield WaitForSeconds(5f);
go.gameObject.GetComponent(PlayerController).speed = 20;
}
then call it when you get the collision:
function OnTriggerEnter(otherObj: Collider)
{
if (otherObj.tag == "Player")
{
Speed(otherObj.gameObject);
}
}