# Character velocity based movement

I am trying to create velocity based movement using a character controller and I am running into a problem.
My Goal:
I want to make sure that player input isn’t increasing the velocity of the controller if the velocity is already greater than my characters max speed.

My problem:
With the below code, I have been able to meet my goal. Except, now an issue has arisen.
if the characters velocity is greater than the max speed, then the player has no control over the character at all. I would like if the player moves in an apposing direction that it decreases the velocity of the character, even if the velocity of the character is over the max speed.

I hope all of this has made sense…
I have a feeling that I am greatly over complicating everything

This is a mock up of what I have done so far and doesn’t reflect all of my movement code.

``````public void Update()
{
//I don't want to take the y of velocity into account when measuring the speed of the Character for the            //purposes of player input
Vector3 velocityXZ = new Vector3(velocity.x, 0, velocity.z);
if (velocityXZ.sqrMagnitude > speed * speed)
{
Vector3 dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
inputVelocity = dir * speed * Time.deltaTime;
Vector3 velocityChange = velocity + inputVelocity;
if (velocityChange.sqrMagnitude > speed * speed)
velocity = inputVelocity - velocity;
else
velocity += inputVelocity;

controller.Move(velocity * Time.deltaTime);

}
}
``````

You are better off using a rigidbody instead of a character controller if you are going to involve physics

Ok then how would you go about solving this problem for a controller using a rigidbody?

My system is in place for a CharacterController and works as well as a Rigibody would for my application. I am at a crossroads where I would have the exact same problem with either.

If I were using a rigidbody I would still want it to respect external forces and not have player control speed it up past the characters maximum movement speed.

This has been cobbled together at work and I haven’t been able to test it but it’s the general gist…

``````private void FixedUpdate()
{
Vector3 velocity = rb.velocity
//I don't want to take the y of velocity into account when measuring the speed of the Character for the            //purposes of player input
Vector3 velocityXZ = new Vector3(velocity.x, 0, velocity.z);
if (velocityXZ.sqrMagnitude > speed * speed)
{
Vector3 dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
inputVelocity = dir * speed * Time.deltaTime;
Vector3 velocityChange = velocity + inputVelocity;
if (velocityChange.sqrMagnitude > speed * speed)
else

}
}
``````

Would this be of interest to you? Unity - Scripting API: Vector3.ClampMagnitude

I agree that you shouldn’t mix the two types into 1 controller like that.

I have tried using ClampMagnitude. It works well other than the fact that when I move my character while it’s being affected by an external force ( an explosion for example) it is then snapped to the speed of the characters movement when the player attempts to move…

Have a “local” velocity that is entirely input dependent and a “world” velocity that allows outside influences such as boost pads or moving platforms. Clamp the local velocity and combine it with the world velocity for the player’s final velocity.

2 Likes

That does seem like the simplest solution. I’ll give it a shot.

Can you please post an example, im pulling out my hairs trying to get this to work

This thread is over two years old mate. Make a new one with your exact issue and the code you’ve tried.

ok ill do that tomorrow, thanks for replying