My bro and I are having a serious problem with this Unity asset.
https://assetstore.unity.com/packages/tools/physics/kinematic-character-controller-99131?srsltid=AfmBOor2eLxcafBRs5kLH27sFyOMo7TgzlfmYddqkrnpMelUAgcgu87F
Despite our efforts, we can't seem to make an animated character work with it.
#Kinematic Character Controller Woes.
1 messages · Page 1 of 1 (latest)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
using KinematicCharacterController.Examples;
public class ExampleCharacterAnimator : MonoBehaviour
{
public ExampleCharacterController chara;
public Animator anim;
public Vector3 posDiff;
// Update is called once per frame
void Update()
{
anim.SetFloat("Speed", (transform.position-posDiff).magnitude*Time.deltaTime*60*60);
anim.SetBool("Grounded", chara.Motor.GroundingStatus.IsStableOnGround);
posDiff = transform.position;
}
}
This is the code my bro made to try to handle the animation.
Can you do what you've been suggested to do?
Checked what’s going on in animator at runtime.
Showed animator screenshot.
Checked if animation was set to looping. It is.```
These are the suggestions I have received.
"Speed Numbers are fluctuating wildly between a small amount and double digit values"
-Chirz
Did I miss any suggestions?
You never answered of showed a screenshot that shows the animator states at runtime when the issue happens.
Even this screenshot cuts half of the animator out.
Never answered?
I don't see shit in that screenshot. The states names are too blurry.
Does it stay in that first state? Does it seem to loop it? Or is it's progress stuck at the start of the state?
In runtime, the bar in the first state highlighted in orange (which is labelled "Idle Walk Run Blend") doesn't seem to fill at all.
There's a blend tree, but the Speed value is going ALL over the place
There's a blend tree, but the Speed value is going ALL over the place
I'd maybe put a breakpoint in the code to see what your speed calculation evaluates to and why.
The kinematic character controller is likely moving your transform out of sync. It could be that you need to do your speed calculation in FixedUpdate, or set the posDiff in LateUpdate after the character controller has finished updating the transform.
The latter would be the first thing I'd try before digging into the character controller code.
Okay, changing Update to LateUpdate seems to have stabilized the speed value, but it's stuck at the first frame of the run animation.
Chirz is investigating further.
So we've resolved that mystery. But the animator was designed for a different script, and without the values we need exposed, I can't figure out how to make the animator work right
Someone will inevitably tell you to post in #🏃┃animation so I might as well be the one 😅 You've also got a lot more animator experts lurking over there
Isn't there a velocity or speed variable that the KCC outputs instead of trying to calculate that position delta? That's probably why the value is fluctuating on a frame by frame basis
Well, changing the Update function to LateUpdate stabilized the fluctuating value.
(And there were some other things my bro did to the code.)
This is the script that my bro wrote to handle animations.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
using KinematicCharacterController.Examples;
public class ExampleCharacterAnimator : MonoBehaviour
{
public ExampleCharacterController chara;
public Animator anim;
public Vector3 posDiff;
// Update is called once per frame
void LateUpdate()
{
anim.SetFloat("Speed", ((transform.position-posDiff).magnitude/Time.deltaTime)/4);
anim.SetFloat("MotionSpeed", ((transform.position-posDiff).magnitude/Time.deltaTime)/10);
anim.SetBool("Grounded", chara.Motor.GroundingStatus.IsStableOnGround);
posDiff = transform.position;
}
}
Here's what it looks like now.
Also, my bro said he couldn't find any exposed variables in the KCC code.
If you could help him with that, that would be great.
In the SetFloat function, why are you guys dividing the position delta by Time.deltaTime and then dividing again?
Definitely something to ask @split raft when he wakes up again.
It’s just because the resulting value was way too high for the animator. As mentioned before, the animator was meant for another script.
In your ExampleCharacterController script, there is a public variable called Motor. You can get the player's current velocity by using Motor.BaseVelocity.magnitude (or sqrMagnitude if you want to optimize). That float value should be a more stable and correct speed value
So you can just set this code to
anim.SetFloat("Speed", Motor.BaseVelocity.magnitude);
And then in your Blend Tree, set whatever the max speed is to be the upper limit of the blend.