Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges