Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Mar 03, 2017 at 02:40 PM by PresidentPorpoise for the following reason:

Other

avatar image
1
Question by PresidentPorpoise · Feb 19, 2017 at 10:56 PM · 2dscripting problemphysicsscripting beginnerphysics2d

Limit player speed (2D)

Hello, I am prototyping a 2D platforming game for practice. I am working on the player movement and so far I have successfully gotten it to move based on input. However, I am unable to limit the speed. I have tried setting a max speed and changing the velocity to match the max speed if the player's current velocity exceeds the max speed, but that has not been working. Is there a smooth, clean way to limit the max speed? Can I clamp it? If so, how? I am very new to programming, especially when it comes to 2D physics. Thanks in advanced.

Here is my current script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float speed;
     public float maxSpeed;
     public bool onGround;
     public Rigidbody2D playerRB;
 
     private void Start()
     {
         playerRB = gameObject.GetComponent<Rigidbody2D>();
     }
 
     private void FixedUpdate()
     {
         float x = Input.GetAxis("Horizontal");
         playerRB.AddForce((Vector2.right * speed) * x);
 
     }
 
 }
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

  • Sort: 
avatar image
1

Answer by Determined · Feb 20, 2017 at 05:58 AM

Have a look at this answer.
http://answers.unity3d.com/questions/1035491/how-to-clamp-set-max-value-the-velocity-of-a-2d-ri.html

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

Answer by Ninjapro2k_ · Feb 20, 2017 at 12:56 AM

Welcome to 2D physics, PresidentPorpise :) I ran your code against an old one of mine (stripped down to the basics). When I tested yours, it definitely kept increasing in speed (with speed at 10 and maxSpeed at 15). Acceleration was a bit slow.

For some reason mine has sharper acceleration, and won't accelerate past the set float value. I'm also pretty new, so I'm sure there's a tutorial that could explain more.

using UnityEngine; using System.Collections;

public class MyMovement : MonoBehaviour {

 public float maxSpeed = 10f;
 private Rigidbody2D rigid2d;

 void Start()
 {
     rigid2d = GetComponent<Rigidbody2D>();
 }

  void FixedUpdate()
 {
     float move = Input.GetAxis("Horizontal");

     rigid2d.velocity = new Vector2(move * maxSpeed, rigid2d.velocity.y);
     Vector2 movement = new Vector2(move, 0);
     rigid2d.AddForce(movement * maxSpeed);
 }

}

If you're interested in clamping values, use Mathf.Clamp. It's very easy to use, and efficient :) Good luck on your game

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 aditya · Feb 20, 2017 at 05:09 AM 0
Share

actually there is a problem with the above code ... you are setting both velocity and addforce in each fixed update, either set velocity or addforce

avatar image Kossuranta · Feb 20, 2017 at 07:24 AM 0
Share

Your script has "sharp" acceleration because you set your velocity directly, this also keeps player speed constant.

avatar image
0

Answer by Anupam13 · Feb 21, 2017 at 09:40 AM

Try this- if(playerRB.velocity.magnitude>maxSpeed){ playerRB.velocity=playerRB.velocity.normalised*maxSpeed}

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

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I slow the velocity of my player without it affecting fall speed? 0 Answers

Maintain equal physics over different UI sizes? 2 Answers

Cannot set motor speed in script 1 Answer

Why does my character pass through walls when he respawns and I lose control of him? 2 Answers

Object jumps right after the attached HingeJoint2D is Enabled 0 Answers


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