• 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 bracciata · Aug 22, 2015 at 04:33 PM · c#addforcespeed

2d Platformer Horizontal Max Speed

I am currently using GetComponent ().velocity = leftforce ; as my code but the character fall super slow but the problem with addforce right now is they start to go to fast and I'm not sure how to sure to add a maximum speed to add force.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by moriggi · Aug 22, 2015 at 07:40 PM

multiply the code x Time.deltaTime in the horizantal axis and set the speed in the inspector. by

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 Eno-Khaon · Aug 22, 2015 at 09:16 PM

One possible approach is to scale your input relative to your current speed. For example, if you're currently moving at your maximum speed, the force added will be scaled down to 0. This generally also takes a little tweaking to ensure that you're not preventing movement in the opposite direction.

Note: The code's untested, imperfect, and technically built for a 3D environment, but hopefully it may help to give some ideas of how to approach movement speed scaling.

 // C#
 
 public float acceleration; // How fast do you want to reach max speed?
 public float maxSpeed; // The maximum speed you wish to reach. Either don't let it be zero or put in an if statement to avoid division by 0
 Rigidbody rb; // Defined earlier for quicker access
 
 // ...
 
 Vector3 movementInput = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
 
 // ...
 
 float speedScale = Vector3.Dot(rb.velocity / maxSpeed, movementInput);
 speedScale = 1.0f - (Mathf.Clamp01(speedScale));
 
 rb.AddForce(movementInput * acceleration * speedScale);
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to adjust the speed of an object? 1 Answer

Flipping an object using AddForce? 2 Answers

Ball doesn't increase speed. C# 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges