Hi guys and gals , i just watched and followed along with the infinite runner live training tutorial
and have the same end prototype as the video, i wanted to add a few things in of my own but i am a total noob when it comes down to experimenting with the code.
I’d like to make my game so that the power-ups/ pick-ups (whatever you wanna call them) increase the player’s speed for a certain amount of time. i am using the 2dcharacter controller included in the sample assets beta package (the character is always running towards the right on the x axis like in the video)
Any help would be greatly appreciated, i’m using C# as well.
I’m guessing ill have to make a reference in my ‘power-up’ script to my character controller and increase the maxSpeed in the character controller by what ever number.
You don’t need to worry about the power-up having a script only a tag or name that represents the power-up.
Power-up should be an image with a collider set to trigger. The name could be “Speed” or “Speed20” for different speed boosts. Then on the player make sure you have a rigidbody2D attached or the trigger won’t “trigger”. Then in a script on the player add
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.name == "Speed")
{
//Put code here to add to the speed. Start a timer so it only last so long.
}
}
Awesome stuff Chris, thank’s a lot! i have another question you might be able to help me on, how do i set a timer? like i said, i am an absolute noob!
thanks
float timerLength = 10.0f;//The length of time in seconds.
float timerTimePassed = 0.0f;//The variable that will store the time passed while the timer is going.
bool runTimer = false;//Used to start/stop the timer.
void Update()
{
if (runTimer)
{
timerTimePassed += Time.timeDelta;
if (timerTimePassed >= timerLength)
{
timerTimePassed = 0f;
runTimer = false;
//Put code here to be triggered when timer fires.
}
}
}
All you need to do is set runTimer to true when you collect the pickup in the OnTrigger function.
You could also look into Coroutines as well. Do some research on timers you may find a better way than what I’ve been doing for years.