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 /
avatar image
0
Question by kinga12nagy · Nov 19, 2018 at 10:46 AM · playerprefabscollision2dspeed issues

Player's speed after collision problem

Hi! I have a player and a Berry prefab. If I collise with the Berry, the player's speed will double for 1s and progressively slow down and the player will get back it's regural speed. I used coroutine and my code works well (I think XD). But I have a problem with touching berry prefabs one after another... The actual and final speed is written out, and I get really strange results... If the player touches the 1st berry and 1s later the 2nd berry, the Final speed is 4 instead of 5. I get strange result... Can you help me to find where the problem is?

 public class PlayerController : MonoBehaviour {
 
     private float runSpeed;
     private float jumpSpeed;
     private float moveX, jumpY;
 
 
     //Definition of objects with triggers
     public Collider2D berry;
 
     private void Start()
     {
         runSpeed = 5f;
         jumpSpeed = 6.5f;
   
 
     }
 
     void FixedUpdate()
     {
         //*Time.deltaTime means: we want to move this object x meter/second instead of x meter/frame
         moveX = Input.GetAxis("Horizontal") * Time.deltaTime * runSpeed;
         jumpY = Input.GetAxis("Jump") * Time.deltaTime * jumpSpeed;
  
 
         transform.Translate(moveX, jumpY, 0f);
     }
 
     //if player touches the small berry it will be destroyed
     //the speed of the player will be boosted for X seconds
     void OnTriggerEnter2D(Collider2D berry)
     {
         Debug.Log("The berry is touched");
         Destroy(berry.gameObject);
         StartCoroutine(DoubleTheSpeed());
     }
 
 
     //process for boosting the speed
     //double speed for 1s than slow down
     IEnumerator DoubleTheSpeed()
     {
             runSpeed = 2 * runSpeed;
             yield return new WaitForSeconds(1);
             // slowing down
             while (!(runSpeed <= 5))
             {
                 yield return new WaitForSeconds(0.4f);
                 runSpeed -= 1;
                 print("Actual speed : " + runSpeed.ToString());
                 yield return null;
 
         }
             print("Final speed : " + runSpeed.ToString());
 
     }
 }

And here are the results I got:alt text

logs.jpg (153.2 kB)
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 ecv80 · Nov 19, 2018 at 01:20 PM 0
Share

I get a glimpse of what's going on. But I don't really get what you want to actually happen. Please elaborate further, be more specific.

Right now anytime you collide with a berry, your player speed doubles whatever its speed currently is. After 1s speed will be checked and if it's greater than 5f (dang man! isn't runSpeed>5f much more readable than !(runSpeed

I can think of a couple reasons why this might not be the behavior your intended it to be. But I'd need you to be more specific about it: Should the speed doubling be cumulative when you pick more than one berry? Should the speed be reduced always at the same pace no matter how many berries have been picked? If that's so, right now for every berry you pick, the speed will double cumulatively, but also the speed will be then reduced cumulatively, so long as after the allowed 1s the speed is still over 5. There's also room for possibly unintended behaviors as the first speed reduction will be carried out after 0.4s, at which point, other "concurrent" coroutine executions might have already reduced the speed below the check threshold.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mlnczk · Nov 19, 2018 at 01:27 PM

Well you can workaround it with boolean. Set bool "isBoosted" and check in Update if isn't boosted then set runspeed = 5.0f. Propably not the best way but should work. Cheers!

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

114 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do the player's health points go down so fast? 1 Answer

Detect and Recursively Space Out Platforms in 2D Endless Runner 1 Answer

Prefab behaviour 1 Answer

How can assign just 1 specific prefab to 1 specific player?(Multiplayer) 0 Answers

Same duplicate prefab not work. 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