How do I get my character to crouch?
There are two major aspects to this that I don’t know how to resolve. One is to adjust the collision of my character. I need a smaller collision box so that higher attacks will not hit the character, and so that the character can walk under low ledges.
The second issue is making sure the player doesn’t fall. The player’s position is determined by its center. If the player is suddenly made half the size, the player would be suspended in the air about a quarter of his height off the ground. This should be avoided.
(I’m actually working in 2D, but I’m pretty sure the concept will work the same in both 2D and 3D)
I remember the unity vids for the mechanim system have a section on changing the collider size when the robot thing does a barrelroll jump if that helps… should be on the learn section of the website I think
Hmm, I just get the error that GameObject doesn’t have a definition for Collider. I guess I need to cast it or something, but I don’t know the syntax for that. I’m still new to Unity.
I tried looking for that video but I found nothing like that among the tutorials.
Okay, for some reason this didn’t just modify my collider, it modified the whole character. When I crouched, I instantly teleported to the world origin.
(It took me a while to realize what was happening because I had a few other things set up wrong and so I suddenly saw nothing at all.)
By commenting lines out, I have discovered that altering either the scale or the position will result in my being teleported to the world origin. If I comment out both of those, no teleportation occurs, so I know it’s the changing of the collider that’s doing this.
The transform attached to the collider is the same as the one attached to the GameObject - so you’re actually modifying the scale and position of the GameObject.
(Even so, why would adjusting only the scale reset my position?)
Thankfully, I am only using a single box collider for my collision. What is the syntax to cast the BoxCollider2D attached to my game object, so I can modify its properties directly? I’ve never done this in Unity.
As I had said, even if I comment out the position change (so it is only the scale change) I still teleport to the world origin.
Well, I have the crouching code working now. Thanks for your help!
The next step is to check to make sure the player has room to stand up before standing. I think I can manage that by doing the same thing I did to check if the player is standing on ground.
The code, in case anyone is interested or is reading this thread
I actually found a simpler solution to crouch on /r/unity2d. Basically it’s just toggling my box collider on and off.
I recommend that you have two colliders for your player, if you don’t already. A top box collider for hit detection and a bottom circle collider to make movement more fluid over angles and stairs. This just disables the top collider, although you could probably switch in a smaller box collider if need be.
public GameObject playerObject;
void Update()
{
if (Input.GetButton ("/*BUTTON NAME GOES HERE*/"))
{
playerObject.GetComponent<BoxCollider2D>().enabled = false;
}
else
{
playerObject.GetComponent<BoxCollider2D>().enabled = true;
}
}