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 iamananimal · May 31, 2013 at 11:44 AM · updateontriggerenterspeedincreasedecrease

How to increase the speed of the character in OnTriggerEnter function?

Hi everyone,

I'm trying to make a game similar to something like Doodle Jump: The character move automatically up, with its speed that progressively decreases.

The issue that I encounter is with the stars, which are power-ups that boost speed for a small amount of time. I tried a lot of different things in the function OnTriggerEnter but it didn't work at all (except adding force to the rigidbody, but consequently I have the game over GUI that appears even if the character still moves, since it thinks that currentSpeed == minSpeed in the Update without taking into consideration the rigidbody.AddForce in the OnTriggerEnter).

There's my code:

 // Speed of the Player (left and right movements)
 var moveSpeed = 10f;  
 var movement;
     
 
 // Decrease-Increase speed (up)
 var maxSpeed = 30.0;
 var minSpeed = 0.0;
 var maxAcceleration = 1.0; // Controls the acceleration
 var maxAcceleration2 = 8.0; // Controls the acceleration
 var currentSpeed = 30.0;
 //var currentSpeed2 = 30.0;
 
         
 // GUI that appears when Game Over    
 var showGUI : boolean = false;
 
 var iconGO : Texture2D;
 var iconRetry : Texture2D;
 var iconBack : Texture2D;
 var positionGO :  Rect;
 var positionRetry :  Rect;
 var positionBack :  Rect;
             
                     
     
 function Update() 
 {  
     // gameplay Player (left and right)
     movement = Input.GetAxis("Horizontal") * moveSpeed;  
     movement *= Time.deltaTime;  
     transform.Translate(0.0f,0.0f, movement);
     
     
     // moving automatically up
     transform.Translate(Vector3.up * Time.deltaTime * currentSpeed);
     
     // decreasing speed
     if (currentSpeed > minSpeed)
     {
     currentSpeed -= maxAcceleration * Time.deltaTime;
     currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
     }
     
     // when the character stops, GUI Game Over
     if (currentSpeed == minSpeed)
     {
     //Debug.Log ("YES"); //working (currentSpeed == 0.0)
     showGUI = true;
     }
     
 }
     
             
 function OnTriggerEnter(other: Collider)
 {
     if (other.tag == "Stars")
     {
         // Works but the GUI appears before the character stops (when it takes star(s)):
         //rigidbody.AddForce (0, 300, 0);
         
         //Debug.Log ("YES 02"); //working (collision)
         
         // one of the many tests that I tried:
         transform.Translate(Vector3.up * Time.deltaTime * maxSpeed);
         maxSpeed += maxAcceleration2 * Time.deltaTime;
         maxSpeed = Mathf.Clamp(maxSpeed, minSpeed, currentSpeed);
         
         // an other one that I tried:
         transform.Translate(Vector3.up * Time.deltaTime * currentSpeed2);
         currentSpeed2 += maxAcceleration2 * Time.deltaTime;
         currentSpeed2 = Mathf.Clamp(currentSpeed2, minSpeed, maxSpeed);
         
     }
 }
     
     
     
 function OnGUI()
 {
     if(showGUI)
          
     {
     
         if(GUI.Button(positionGO, iconGO, "label"))
         {
             // Image Game Over
         }
         
         else if(GUI.Button(positionRetry, iconRetry, "label"))
         {
             // Button that reloads the current scene
             Application.LoadLevel("RUNNER");
         }
             
         else if(GUI.Button(positionBack, iconBack, "label"))
         {
             // Button that allows to go back to the menu
             Application.LoadLevel("MENU");
         
         }
         
     }
 }

Maybe, to resolve it easily, is there a code that simply says "show me the GUI once my character stopped (so, not related to currentSpeed and minSpeed in the Update)" ?

Thanks in advance for all the help I can get !

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 Bloodyem · May 31, 2013 at 03:15 PM

Ok, I'm fairly new myself, so I'm kinda guessing, but what I think I would do would make it so when you got a star, it turned on a boolean, and while the boolean was active, currentSpeed = maxSpeed, and then use Time.deltaTime to figure out how long he had been boosted for.

 function OnTriggerEnter(other: Collider)
 {
     if (other.tag == "Stars")
     {
  var start : boolean = true;
 
 
 //Right here is where we have the setup. All collecting the star will do is alter the speed you //are moving, and once 5 seconds has gone by, should revert back to normal. 
            if (star == true)
                {
                      timeCheck = 0;
                      currentSpeed = 50;
                      timeCheck += Time.deltaTime;
                                if (timeCheck >= 5)
                                          {
                                                  currentSpeed = 30;
                                                  star = false;
                                          }
                }
 }
  
        transform.Translate(Vector3.up * Time.deltaTime * maxSpeed);
        maxSpeed += maxAcceleration2 * Time.deltaTime;
         maxSpeed = Mathf.Clamp(maxSpeed, minSpeed, currentSpeed);

Sorry if it's not ideal or perfect, but it ought to work.

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 iamananimal · May 31, 2013 at 03:58 PM 0
Share

Ok hum you don't have to be sorry, it just worked perfectly ;-)

Thank you very much Bloodyem, I feel so dumb to not have thought about something as logical as this..!

-For someone else who could have a similar problem in the future and find this page (even if it's obvious), or if you can edit your answer Bloodyem: the 'var' in 'var timeCheck += Time.deltaTime;' is unnecessary of course ;-)

Thanks again!

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

14 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

Related Questions

Increasing the speed for a few seconds doesn't work. 1 Answer

Smooth increasing and decreasing inside Invoke function? 2 Answers

Increase speed in float over time. 0 Answers

Simple "E" to interact script... 1 Answer

OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 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