Crouch C#

Hi guys i need some help.
I’m good at scripting trying to learn it.
So i’m making crouching script :

public bool Crouch;

public void Update()
{

		if(Input.GetButtonDown("Crouch"))
           {
              //Only apply  to collider(CharacterController)

              transform.localScale -= new Vector3(0,0.5F,0);
              Crouch = true;
           }
		else 
		{
                         //Get Back to same position?
			Crouch = false;
		}
	}

Any clue how to make crouch only to the mesh not whole character, and how to get it back to same position.
Thank you

If it’s a fps cam just move the cam local pos. but if you have a avatar you will need a crouch animation. In any case your just dealing with height of the character Controller.

hey, I sent you a pm.

Can you be more specific about it i know i need to learn it my self but i’m still a beginner .Any tips?

Generally crouching is done through animations rather than squashing the model.

If you want to do the squashing thing, then rather than subtracking scale, just set it to a value you want.

Make sure you assign the mesh to the transform.

Transform targetMesh;

void Update()
{
  if(GetButton("Crouch"))
     targetMesh.localScale = new Vector3(1,0.5f,1);
  else targetMesh.localScale = new Vector3(1,1,1);

}
1 Like