Hello, i am working on a 2d plattformer game as my first unity project. I have made the player jump and double jump, but now i want him to crouch as well. I was just wondering if someone had any starter ideas how i should start?
Well, if you want a really easy solution, you only need a few things in your script:
A variable that represents the collider size when crouching (aka, how high your character’ll be after crouching)
A key for crouching
Pseudocode:
if crouchKey is pressed then
start coroutine "crouch"
endif
crouch coroutine:
set collider height to crouchHeight
set sprite to crouch sprite
while crouchKey is pressed or there's something above (raycast)
yield return null
crouching = true
end while
set collider height to normal
crouching = false
end coroutine
In the pseudocode I used a coroutine, because it’s easier than:
if crouchKey is pressed or (something is above and crouching) then
...
endif
AFAIK you can set the collider height with GetComponent().bounds = … but I’m not sure
If you don’t know, how you can check if something is above the player, just simply google raycasting
I included a boolean variable in the pseudocode, because you might want to use it, for example in mario games you can’t move if you’re crouching
if you have anymore questions, just ask, and if you don’t know, hoe you would create the code I described, you might want to look at the learn section, there’s surely a tutorial, that teaches you crouching
{
if (Input.GetKey(KeyCode.G))
{
playerObject.GetComponent<BoxCollider2D>().enabled = false;
Debug.Log("works");
}
else
{
playerObject.GetComponent<BoxCollider2D>().enabled = true;
}
}
I’ve looked through lots of tutorials and read somewhere that you can turn of the normal collider and then add a circlecollider at the bottom of the player. But the problem is that i have no idea how that should be done. If you got any suggestion how it could be fixed or even another solution, maybe a tutorial I would really appreciate if you could forward it to me