About power-ups...
Okay, so...
I've been trying to make a game where you are a comet. It's a platformer that doesn't use jumping. But I seem to be having trouble regarding the programing of power-ups. I need two power-ups (for now, anyways): Boosting (raising the speed of the player for a short period of time when he/she collides with a boost panel) and Invincibity (pretty self-explanatory).
My code is as follows:
using UnityEngine;
using System.Collections;
public class BallController : MonoBehaviour {
public float speed;
public float initialSpeed = 10;
public float topSpeed = 40;
private float middleSpeed = 15;
private float direction;
private Rigidbody2D rigby;
public ParticleSystem part;
public GameObject suiEyes;
public GameObject closedEyes;
// Use this for initialization
void Start () {
speed = initialSpeed;
rigby = GetComponent<Rigidbody2D>();
//part.GetComponent<ParticleSystem>();
part.enableEmission = false;
suiEyes.SetActive(true);
closedEyes.SetActive(false);
}
// Update is called once per frame
void FixedUpdate () {
direction = Input.GetAxis("Horizontal");
if (direction != 0)
{
if (speed < topSpeed)
{
speed += 0.1f;
print("speed x:" + speed + " speed y: " + rigby.velocity.y);
}
if (speed > topSpeed)
{
speed = topSpeed;
}
rigby.AddForce(new Vector3(direction * speed, 0, 0));
}
else
{
speed = initialSpeed;
}
if (rigby.velocity.x >= middleSpeed || rigby.velocity.y < -16 || rigby.velocity.x <= (middleSpeed*-1))
{
part.enableEmission = true;
suiEyes.SetActive(false);
closedEyes.SetActive(true);
}
else if (rigby.velocity.x < initialSpeed + 1 || rigby.velocity.y == 0)
{
part.enableEmission = false;
suiEyes.SetActive(true);
closedEyes.SetActive(false);
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "Boost")
{
topSpeed = topSpeed + 100;
}
}
}
Answer by Rangr · Nov 19, 2015 at 02:10 AM
Well, if I'm understanding your needs correctly, I'd suggest using a coroutine for your power-ups. Coroutines would allow you to count out the timers for either power-up independent of the timing for the rest of the code. For example, if the player hits a boost power-up, trigger a coroutine that first, sets the topSpeed to the boost speed (topSpeed + 100?), then have a for loop to stall for the duration of the power up that simply has a return line in it. After the loop, finishes running, change the topSpeed back to normal. Something similar would work for invincibility. Set a boolean at the beginning of the loop, if the player would have been damaged during the extent of the loop, check the boolean and if they are invincible, they don't take the damage. If you need the documentation on coroutines, here it is: http://docs.unity3d.com/Manual/Coroutines.html
Hope this helps and best of luck!
Thanks, man! You really helped me out here! I'll try it out as soon as I get home.
Your answer
Follow this Question
Related Questions
c# help with rotate and rotatearound 0 Answers
coroutine or update for powerups cooldown 0 Answers
Slow Motion Effect on Trigger 0 Answers
How to make a POWER UP that slows the player down with force. 0 Answers
Questions about PowerUps? 1 Answer