Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TubeSocSamurai · Aug 11, 2014 at 03:44 PM · speedpongpowerup

HELP! With my Speed power up

Im making a pong game with power-ups what i want to happen is that when the ball hits the power up the float speed increases by 5 for 10 seconds or until a point is scored but whats happening is that the speed increases by 5 after a point is scored not immdiately when its hit heres my code

 using UnityEngine;
 using System.Collections;
 
 public class PowerUp : MonoBehaviour {
     static bool trip = false;

     void OnCollisionEnter2D(Collision2D other){

         if(other.gameObject.tag=="ball"&&!trip){
             trip=true;//stop collision from running twice
             ballControl.speed+=10;
             StartCoroutine(speedtime());
             GetComponent<SpriteRenderer>().enabled=false;
         }
     }
     
     IEnumerator speedtime(){
         yield return new WaitForSeconds (1);
         revertSpeed();
         Destroy (gameObject);
     }

     void revertSpeed(){
         ballControl.speed = 5;
     }
 }
Comment
Add comment · Show 1
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 tanoshimi · Aug 11, 2014 at 03:48 PM 0
Share

There's nothing in that code that would either increase the speed by 5 or do anything when a point is scored, so it rather suggests you've got a problem in a different block of code.

2 Replies

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

Answer by DajBuzi · Aug 11, 2014 at 03:54 PM

Hello,

I dont know if that's what you need but since it's some kind of power up then use triggers instead of collisions:

 void OnTriggerEnter2D(Collider2D col) //so that the ball will not bounce off

Second thig is when it hits this trigger you should start counting:

 float someTime;
 bool PowerUp = false;
 public void Update(){
     if(PowerUP){
         someTime += Time.deltaTime
         if(someTime >= 10.0f) PowerUP = false;
     }
 }

In the OnTriggerEnter2D method just simply change this values like so:

 if(col.tag == "PowerUP"){
     someTime = 0;
     PowerUP = true;
 }

Regards, M.Rogalski

Comment
Add comment · Show 2 · 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 TubeSocSamurai · Aug 11, 2014 at 04:19 PM 0
Share

Thank you Thank you that helps alot but arises another question where would i add a value for the bool "PowerUp"?

avatar image DajBuzi · Aug 12, 2014 at 02:30 PM 0
Share

Add it where it will be accessible eg. to the ball :)

avatar image
0

Answer by TubeSocSamurai · Aug 13, 2014 at 01:22 PM

ok i did i reworked my code but now i've run into another problem when it hits the powerUp it doesn't immediately take effect it waits until after a point has been scored to work here my on trigger code

 void OnTriggerEnter2D(Collider2D col){
         if (col.gameObject.tag == "Pickup") {
             SetBallSpeed(speed*2);
             Destroy(col.gameObject);
             }
     }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Increase the speed after collision 0 Answers

How to freeze movement on X-axis. (See Comments for Update) 2 Answers

Power Up speed and jump boost 2 Answers

Rotate object from localRotation A to localRotation B at rate C. 2 Answers

Collider problems with Speed 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