Cap rigidbody movement speed based on input.
Hello,
I have a character controller that uses the input axis to determine the direction of the rigidbody. With a controller it works fine. But with keys the character moves extra fast when the horizontal and vertical axis are pressed. This isn't surprising, because with a controller the X and Y axis can never be more than 1 when you add them. But with keys they can both be set to 1, which means that the character will get force on the X and Z axis at full power. So it moves faster.
So what's a good way of capping them without a delay? I have this now, but it does have a slight delay.
// In the update
void Update()
{
//so this is supposed to cap the axis.
//hor and ver are the axis from the input. ( I.E. Input.GetAxis(Horizontal))
if((hor == 1f || hor == -1f) && (ver == 1f || ver == -1f))
{
hor = hor * 0.5f;
ver = ver * 0.5f;
}
}
//Late update for physics.
private void LateUpdate()
{
// Adds the force to the character.
rigid.AddForce(new Vector3(hor, 0, -ver) * status.speed);
}
Answer by markdavidmorris · Jun 15, 2016 at 04:11 PM
From what you've shown there shouldn't be a delay. By delay do you mean it moves at full power and then gets cut to half power? I don't see why it would ever reach full power (1 horizontal and 1 vertical) with what you have.
The player can move on the X and Z axis, so when both are set to 1, the player is faster because of the combined force.
The delay may be caused because the axis isn't immediately set to 1? Twice 0.9 is maybe what causes the slight delay.
Your answer
Follow this Question
Related Questions
Unity 5 arrow shooting 2 Answers
rigidbody.AddForce doesn´t work in c# 1 Answer
How to apply a force at certain point of an object? 0 Answers
Rigidbody looses momentum with contact to ground. Why? 3 Answers
Applying Force to the Character 0 Answers