- Home /
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.
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
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);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help on Player Movement Script C# 0 Answers
Flight Sim Control 1 Answer
How do i spawn these cloned gameobjects with varying speeds??? 2 Answers