How do I crouch?

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)

1 Like

You just need to adjust the size of your collider. Reduce its size by half and change it’s position so your character will rest on the ground.

Okay, but how do I do that?
Do you have a coding example I can look at?

java or c# ?

The rest of my code is in c#, but honestly, I just don’t know how I can make changes to my collision via code. I’m still new to Unity.

Try this for changing the collider size:

gameObject.Collider.transform.localScale( new_size_vector3);

and for changing its position:

gameObject.Collider.transform.localPosition( new_position_vector3);

new_size_vector3 and new_position_vector3 needs to be Vector3 values.

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.

in c# “collider” is lowercase. So, how about something like this:

Vector3 scale = gameObject.collider.transform.localScale;
scale.y *= 0.5f;
gameObject.collider.transform.localScale = scale;

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.)

	void Crouch() {
		bIsCrouched = true;
		gameObject.collider2D.transform.localScale = new Vector2 (1, crouchHeight/standHeight);
		gameObject.collider2D.transform.localPosition = new Vector2 ( 0, crouchOffset);
		anim.SetTrigger("Crouch");

	}

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.

Collider2D won’t give you what you need out of the box - you’ll have to cast to one of the Collider2D derivatives to expose things like http://docs.unity3d.com/Documentation/ScriptReference/BoxCollider2D-center.html and http://docs.unity3d.com/Documentation/ScriptReference/BoxCollider2D-size.html

(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.

You’re not just modifying the scale. Your second line modifies localPosition.

Use GetComponent to get the BoxCollider2D or cast gameObject.collider2D. (GetComponent is more standard IMO)

BoxCollider2D c;
c = GetComponent<BoxCollider2D>();
c = (BoxCollider2D)gameObject.collider2D;
c = gameObject.collider2D as BoxCollider2D;

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

	BoxCollider2D PlayerCollision;
	float crouchHeight = 3.5f;
	float standHeight = 5f;
	float crouchOffset = -0.75f;

...

void Update () {

		...

		if(bGrounded  Input.GetButtonDown("Fire3"))
		{
			Crouch();
		}

		if(bIsCrouched)
		{
			if (Input.GetButtonUp("Fire3") || !bGrounded)
				StandUp();
		}

		...
	}

...

	void Crouch() {
		bIsCrouched = true;
		PlayerCollision.size = new Vector2 (PlayerCollision.size.x, crouchHeight);
		PlayerCollision.center = new Vector2 (0, crouchOffset);
		anim.SetTrigger("Crouch");

	}

	void StandUp() {
		bIsCrouched = false;
		PlayerCollision.size = new Vector2 (PlayerCollision.size.x, standHeight);
		PlayerCollision.center = new Vector2 (0, 0);

What are “bGrounded” and “bIsCrouched”? Bools?

*edit because I can’t fucking read

if(Physics.Raycast(transform.position, Vector3.up, PlayerCollision.height/2))
{
    ///can't stand up
}
else
{
    ///can stand up
}

Have a raycast shoot up with the length as one half the characters height. If it hits something you can’t stand up. If it doesn’t you can stand up!

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;
        }
}
1 Like

if you want i need how to editt game multiplayer sauf glitch

This is a tutorial that will show you how to crouch as well as crawl. It follows the same basic logic other people are saying of adjusting colliders.