So for unity I got the Character Creator 2D. I created a character and baked prefab, and then imported all the bits and pieces into a new Unity project so I could see how it worked. I realized I had to create my own transitions for the animations as well as setup the input system for them to work. As I was setting up the animations I ran into an interesting problem that took a little bit of digging and work to figure out. When going from idle or walk to run I couldn’t find a way to hold down the shift key to switch to the run animation. The problem is that the button press worked as a single press event that once was on, you had to go back and set the animation to false, which in turn would require you to press the button again to achieve the run effect. The result was that the control felt very unnatural compared to other games where the mechanic simply lets you hold down the run key and releasing the key put you back into a walk state (and animation).
After A LOT of hunting I found a solution that worked and felt natural and was not difficult to code as it was built into the input system already. That code was Keyboard.current.leftShiftKey.IsPressed(). I found it because this link: docs.unity3d.com/Packages/com.unity.inputsystem@1.3/manual/HowDoI.html
Pointed out there is a wasPressedThisFrame function that could be used to determine if the key was pressed on this frame and I thought I could kajigger that into some sort of smooth transition. It wasn’t what I thought it was but did lead me to playing around with auto-complete in Visual Studio 2022 where I just pressed the dot after leftShiftKey and “found” that IsPressed() function.
Ok finally my question is: *
Is there some way to find these little tidbits easier?
- I ended up on docs.unity3d.com and found the IsPressed() function, but there’s not really a great explanation of how it could be used and the fact that I found out how to use it is almost by accident. If anyone has any ideas or some link to some docs where this kind of info could be more easily found or read that would be great. Please let me know. In any case I also would like to share the code I used to switch between walk/run/sprint just in case someone else finds this useful. The great thing about it is that I don’t need to setup anything extra in Unity besides just having the animations already and the character creator 2d already has them baked into the prefab.
Code:
void Walk()
{
// if (!isRunning)
if(!Keyboard.current.leftShiftKey.IsPressed() && !Keyboard.current.leftCtrlKey.IsPressed())
{
Vector2 playerVelocity = new Vector2(moveInput.x * walkSpeed, myrigidBody.velocity.y);
myrigidBody.velocity = playerVelocity;
playerHasHorizontalSpeed = Mathf.Abs(myrigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("isWalking", playerHasHorizontalSpeed);
myAnimator.SetBool("isRunning", false);
myAnimator.SetBool("isSprinting", false);
} else if(Keyboard.current.leftShiftKey.IsPressed())
{
Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myrigidBody.velocity.y);
myrigidBody.velocity = playerVelocity;
playerHasHorizontalSpeed = Mathf.Abs(myrigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("isWalking", false);
myAnimator.SetBool("isRunning", true);
myAnimator.SetBool("isSprinting", false);
} else if(Keyboard.current.leftCtrlKey.IsPressed())
{
Vector2 playerVelocity = new Vector2(moveInput.x * sprintSpeed, myrigidBody.velocity.y);
myrigidBody.velocity = playerVelocity;
playerHasHorizontalSpeed = Mathf.Abs(myrigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("isWalking", false);
myAnimator.SetBool("isRunning", false);
myAnimator.SetBool("isSprinting", true);
}
}
Thanks for the help in advance!