- Home /
Using Blend Trees, I want to switch smoothly based on input, but my input is simulated. How can I achieve this?
I've seen this topic discussed a few times before, but no one really seems to provide a solid answer to it. Here's my own take and problem: I've got three control states - (-1, 0, 1). -1 is backwards, 0 is idle, and 1 is forward.
Initially, I just used a direct button press. Down = 1, up equaled -1, and states between caused idle to occur. That required somewhat of a complex animation tree, with ineffectual smoothing between animation states.
Now, I have started using Blend trees - however, direct switching between the three states causes abrupt animation changes. I'd like to smooth that out, but I'm not sure of the best way to do so.
This is what I have come up with so far:
private void PlayerControlSystem()
{
if (ControlKeysPressed)
{
ControlSwitch = Mathf.Lerp(ControlSwitch, 1, 0.1f * Time.deltaTime);
}
else
{
ControlSwitch = Mathf.Lerp(ControlSwitch, -1, 0.1f * Time.deltaTime);
}
}
It partially works, but has some problems such as when it hits zero it should idle, but keeps trying to walk/run as the value itself is actually 0.4456457f (to infinity). I'd like to create a quick and smooth tween between animations instead and it properly stops when the controls are at correct values.
How can I accomplish this?
I've discovered part of the problem. It's never actually reaching zero, despite the states in the tree being -1, 0, and 1 itself. I tried switching it around and using the built-in lerp, as well as assigning all positive values, but the problem still persists even with this:
if (ControlSwitch == 0)
{
_anim.SetFloat(_animHashes.$$anonymous$$ovement, 0, .35f, Time.deltaTime * 2);
}
if (ControlSwitch == 1)
{
_anim.SetFloat(_animHashes.$$anonymous$$ovement, 1, .35f, Time.deltaTime * 2);
}
if (ControlSwitch == -1)
{
_anim.SetFloat(_animHashes.$$anonymous$$ovement, 2, .35f, Time.deltaTime * 2);
}
Answer by Zoogyburger · Feb 24, 2016 at 06:15 PM
So you have an animation controller for your player and have a transition between Idle and Walking. Click on that transition and in the Inspector the will be settings. Grab and drag the blue bar on the timeline all the way to 0:00. If that doesn't work, uncheck Has Exit Time.
No offense, but I think you completely misunderstood the question. I'm already using an animation controller, and I have a blend tree set up to handle the walking animation variants. I don't want abrupt changes that unchecking 'has exit time' would create.
Answer by DevMerlin · Feb 24, 2016 at 06:55 PM
Got it! Okay, so I was on the right track, but I had to add in some additional locks to prevent undesired movement beyond some basic conditions. Essentially, I had one method that was overriding the others, even though it appeared to be 0 it was just changing to fast for me to see.
Your answer
Follow this Question
Related Questions
Mathf.Lerp supposed to be in Update()? 2 Answers
Multiple Cars not working 1 Answer
Mathf.Lerp not working, why? 1 Answer
Distribute terrain in zones 3 Answers
how to use time on lerp 2 Answers