• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Soodaz · Nov 18, 2015 at 10:44 PM · uppoweruppower-uppower

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;
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Soodaz · Nov 19, 2015 at 10:51 AM 0
Share

Thanks, man! You really helped me out here! I'll try it out as soon as I get home.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

c# help with rotate and rotatearound 0 Answers

Double Points Powerup 1 Answer

Slow Motion Effect on Trigger 0 Answers

How can I spawn an item after killing a wave of 6 enemies?,How can I spawn an item after clearing a wave of 6 enemies? 0 Answers

Problems with power up 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges