Moving player with Character Controller and Vector3 SmoothDamp causes incredible movement speed

giddygroundedarmadillo

It’s quite a simple movement script, I’m probably missing something but I really don’t know what it is. I also checked every CharacterController parameter like Skin Width, so neither of them helped. Maybe it’s because I’m using the same velocity that SmoothDamp gives me back, but even if it’s the reason, I still don’t know why.

if (newMoveDirection.magnitude >= 1f) newMoveDirection.Normalize();

            Vector3 moveDirection = new Vector3(newMoveDirection.x * _movementDataSO.SideMoveSpeed, 0f,
                newMoveDirection.y * _movementDataSO.ForwardMoveSpeed);

            bool groundedPlayer = _characterController.isGrounded;
            if (groundedPlayer && _gravitationalVelocity < 0) _gravitationalVelocity = 0f;

            _gravitationalVelocity += _gravityValue * Time.deltaTime;
           
            _characterController.Move((Vector3.SmoothDamp(_currentVelocity, moveDirection, ref _currentVelocity, _smoothingTime) + new Vector3(0f, _gravitationalVelocity, 0f)) * Time.deltaTime);

What you describes sounds like perhaps you’re mixing a Time.deltaTime scaled quantity with one that is not.

But upon closer inspection…

WOW! O_O

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

Once you have done that, you have to find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like

you are using the function wrong.
Check this out: Unity - Scripting API: Vector3.SmoothDamp

Why don’t you just use something like (that’s basically pseudocode):

_currentVelocity = Vector3.Lerp(_currentVelocity, targetVelocity, Time.deltaTime * _lerpSpeed);
_characterController.Move(_currentVelocity + _gravitationalVelocity);
1 Like

Thank you for the detailed answer. As you mentioned, it’s a good practice to split it up, but I was quite sure that there was a problem with the smoothdamp itself, I isolated it and tried different forms. The below one works perfectly fine, all I did was create an empty vector3,

Vector3 _currentInputVector;

 _currentInputVector =
                Vector3.SmoothDamp(_currentInputVector, moveDirection, ref _currentVelocity, _smoothTime);
            _characterController.Move((_currentInputVector + new Vector3(0f, _gravitationalVelocity, 0f)) * Time.deltaTime);

When I try to smoothdamp the velocity, it acts weirdly.

1 Like

Not surprising considering the size of those lines of code. I could never eason about a multi-line monstrosity like that.

We know SmoothDamp didn’t stop working, so the possibilities are:

  • you misunderstand what it does (go read the docs)

  • you are giving it wrong data

  • you are reading the data it produces incorrectly.

The last two can be addressed with extensive logging: print out the quantities and reason if they are correct.

1 Like