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 MustiKe · Apr 23, 2020 at 05:37 PM · 2d gamespeedlimitinvokerepeating

2D Limit player speed

Hello I am working on a endless runner.I am using InvokeRepeating for adding speed to my player but I have problem with it.I want to limit my max player speed to 20f. How can I do this?

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

3 Replies

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

Answer by Lightning_A · Apr 25, 2020 at 04:14 AM

Assuming you want limit the speed on the x axis

 public float maxVelocity = 20f;
 
 //put this inside your invoke repeating
     if(rb.velocity.x  > maxVelocity)
     {
         rb.velocity = new Vector2(maxVelocity, rb.velocity.y);
     }


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 MustiKe · Apr 25, 2020 at 10:35 AM 0
Share

thanks it's worked.

avatar image
0

Answer by K00KIE · Apr 23, 2020 at 06:18 PM

Hi bud.

If you are using rigidbody to move use:

 Rigidbody2D = rb;
 public float maxVelocity = 20f
 
 void FixedUpdate()
 {
         if(rb.velocity > maxVelocity)
         {
                rb.velocity = maxVelocity
         }
 }

Hope that solves your problem.

If you are using any other way of moving, feel free to let me know. i'll help you.

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 MustiKe · Apr 24, 2020 at 11:21 AM 0
Share

it gives a error that says cannot implicitly convert type 'float' to 'UnityEngine.Vector2'

avatar image Triggerly MustiKe · Apr 25, 2020 at 03:47 AM 0
Share
  Rigidbody = rb;
  public float maxVelocity = 20.0f
  
  void FixedUpdate()
  {
          if (rb.velocity >= maxVelocity)
          {
                 rb.velocity = maxVelocity;
          }
  }
avatar image
0

Answer by Shen-Kit · Apr 24, 2020 at 12:50 PM

If the player is moving up the screen use:

 maxVelocity  = 20f;
 if (rb.velocity.magnitude > maxVelocity) {
     rb.velocity = Vector2.up * maxVelocity;
 }


If they're moving left to right use:

 maxVelocity  = 20f;
 if (rb.velocity.magnitude > maxVelocity) {
     rb.velocity = Vector2.right * maxVelocity;
 }

To increase performance, you can replace the (rb.velocity.magnitude > maxVelocity) with (rb.velocity.sqrMagnitude > Mathf.Pow(maxVelocity, 2))

If this works please mark the question answered. If it doesn't please ask any questions I'm happy to help :)

Comment
Add comment · Show 3 · 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 MustiKe · Apr 24, 2020 at 06:15 PM 0
Share

it gives a error that says Invalid token '=' in class, struct, or interface member declaration.If I change maxVelocity to int or float still doesnt work. I think it happens cuz I am using velocity and float in the same code.Is there any way to limit float without velocity?

sry for bad English :)

avatar image Shen-Kit MustiKe · Apr 25, 2020 at 01:34 AM 0
Share

Sorry I made a mistake when typing that. In the if statement it should say: if (rb.velocity.magnitude > maxVelocity)

A cheaper option is to use if (rb.velocity.sqr$$anonymous$$agnitude > $$anonymous$$athf.Pow(maxVelocity, 2))

As I am not in the unity editor right now, I'm not certain of the capitalisation and things like that, but if you use the autocorrect to find the right function either of those should work.

EDIT: I have updated my original answer now so you can just refer to that

avatar image MustiKe Shen-Kit · Apr 25, 2020 at 10:32 AM 0
Share

thanks for helping I found my answer

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

162 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 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

How to limit the force from player input without limiting speed 2 Answers

how to instantiate UI image in unity2D 1 Answer

Pathfinding with specific end-direction and turning speed limit 0 Answers

InvokeRepeating() not working. 1 Answer

Cap rigid body move speed, without capping rotation 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