- Home /
Not a new question and several years old.
Momentum with Character Controller
Hey guys,
I'm building a simple FPS scene for a demo I am supposed to create for a project. Walking around and rotating and jumping and such are easily achievable by having created a Character Controller with a custom script that controls it.
What I'm looking for though, is some help in understanding how to keep my momentum while midair (while also retaining control via inputs). So if I let go of my input keys right now, the character just stops. Which is fine while on the ground, but not when midair. I'm aware of the isGrounded if statement, so I know I'll have to utilize that to get it to work, but what I need help with specifically is how to -add- my input while maintaining my previous momentum while midair.
Current movement code:
float hor = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");
Vector3 speed = new Vector3(hor, 0f, vert);
speed = Vector3.ClampMagnitude(speed, 1f);
if (cc.isGrounded) {
if (Input.GetButtonDown("Jump"))
velocity = jump;
} else {
velocity += Physics.gravity.y * Time.deltaTime;
}
speed *= walk;
speed.y = velocity;
speed = transform.rotation * speed;
cc.Move(speed * Time.deltaTime);
Thanks very much for any help :) If there's an existing question that asks this specifically, my apologies, I hadn't seen it.
Answer by LukeNukem44 · Apr 10, 2019 at 10:55 AM
Hello. I had this problem for a while, some time ago. There's no solution for it because there's no relation between moving a Transform or Rigidbody and Physics momentum.
So I actually came up with an alternative after a lot of testing. It's really quite simple, but manual. At least it's simple. LoL
Here's what I did: I made it so that when I jump, I use values which are also manipulated by my walk speed (so that it can vary with varying walk speeds automatically), and with that values, I AddRelativeForce in the direction I've already moving in on the ground based on what move keys I'm holding down. I shortcut it by directly taking the WASD axis values. -- Then, I noticed it wasn't really working still (things rarely do in Unity), so I had to make my jump an AddRelativeForce as well. -- The third and final thin you'll want to do is, if two connecting move pointers, eg: W and D, are held down, then you are walking and therefore jumping at a diagonal. You'll want to change the speed to be multiplied by something like 0.75f - 0.8f for the walk speed and the directional jump force, like forward + right, or reverse + left. The upwards jump force never has to alter base on all of this.
It's been a few years since you've posted this, so if you have a grander alternative, I'd love to learn more. -- If you've created a player character that doesn't repeatedly bounce in walks (one of my current problems (I'm dying here)), but rather slides along them as we should, I'd love to learn from you.
Luke
Since OP was controlling their character by manipulating velocity manually, the quick solution was to just have the inputs be dependent on the controller being grounded. Velocity changes would then only happen on the ground and the character would naturally keep momentum in the air.
I still don't know what OP is.
Right, there's a lot wrong with doing that, which I got myself through. $$anonymous$$aking them dependent on being grounded negates your following statement, unless you were adding force, which creates instability. I went through them all.
Hi, this was actually me (now on a different account) who asked this question.
I actually did figure out a way to "fake" velocity with a Character Controller, rather than using a Rigidbody with real velocity. I can post the script below for how I did it for just the movement:
input = new Vector3(Input.GetAxis("Horizontal"), -0, Input.GetAxis("Vertical")); // Get input
input = transform.TransformDirection(input); // $$anonymous$$ake sure input is applied to the forward direction of the characterController, not the forward direction of the World
input = Vector3.Clamp$$anonymous$$agnitude(input, 1f); // clamp input to max 1, so you can't have two buttons pressed like W and A for more speed diagonally
velocity.x += input.x * acceleration; // velocity buildup on the horizontal axis
velocity.z += input.z * acceleration; // velocity buildup on the vertical axis
if (input.x == 0) // if there's no input on the horizontal axis
velocity.x = $$anonymous$$athf.SmoothDamp (velocity.x, 0f, ref xvelocity, friction); // smoothdamp to zero
if (input.z == 0) // if there's no input on the vertical axis
velocity.z = $$anonymous$$athf.SmoothDamp (velocity.z, 0f, ref zvelocity, friction); // smoothdamp to zero
velocity = Vector3.Clamp$$anonymous$$agnitude (velocity, maxSpeed); // clamp the speed so the character doesn't accelerate forever
This works really well for me personally, and keeps momentum when you go in air :) I have no idea how "efficient" this is, but it's basically based on the "high level" idea of how the Sonic franchise handled acceleration back in the SEGA days ^^ Hope it helps :)
$$anonymous$$an! That is awesome! $$anonymous$$any thanks for that!
I've got it all happening except, I'm just wondering what your move method is, like: transform.Translate(velocity); or transform.position etc.
This is especially great because I've always got Unity's Character Controller in the back of my $$anonymous$$d tempted to use it because I was fascinated to learn that it is done without physics. -- This now make me wonder if you still use a Rigidbody for an easy Gravity solution. ? -- Then there's the IsGrounded dilemma which I think I made a great solution to.
I actually use characterController.$$anonymous$$ove for my movement method, sorry that I didn't include that. This code is specifically in relation to be used for the CharacterController (at least, that's how I made it) though you can probably apply it through Translate or transform.position, but not sure how you'd do that well ^^;
So no Rigidbody, just the CharacterController and my own gravity implementation, though that has still a lot of problems so I won't share that here since I need to fix it first.
IsGrounded works well, as long as you make sure the characterController gets pushed to the ground by a small gravity force when you're not in the air, and then a normal gravity force when you're in the air. I haven't been able to get that to work well on slopes tho, hence why I am still working on that.
love you for this but what are you using for the smooth damp vector3 references name xvelocity and zvelocity?
Follow this Question
Related Questions
Character rotates left and right instead of going forward. 1 Answer
Slowly move a GameObject on 1 axis, then destroy it. 1 Answer
New to Unity & Scripting, How Do I Create a Movement System Thats About Building Up Speed? 0 Answers
How to I put a speed limit on my character's movement? 1 Answer
Implementing Counter-Movement 0 Answers