Crouching script need help please

So I have found this crouching script and it works perfectly. The only issue is it does not go low enough for my player so what do I change to make it go lower. (I am a javascript programmer, not c# so do not judge me)

using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class ccrouch : MonoBehaviour {
 
     // PUBLIC VARS //
 
     public Transform t_mesh;                    // Player Transform
     public CharacterController ccr_controller;  // Get the character controller
     public bool IsCrouch = false;               // 
     public float LocalScaleY;                   // Y scale of "t_mesh"
     public float ControllerHeight;              // Y scale of the character controller
 
     // PRIVATE VARS //
 
     void Start()
     {
         //nothing here yet
     }
 
     void Update()
     {
         if(Input.GetKeyDown("c"))
         {
             IsCrouch = !IsCrouch;
             CrouchFunction();
         }
     }
 
    void CrouchFunction()
      {
          if(IsCrouch == true)
          {
              t_mesh.localScale = new Vector3(1, LocalScaleY, 1);
              ccr_controller.height = ControllerHeight;
              Debug.Log("c_func 1");
          }
          else
          {
              Ray ray = new Ray();
              RaycastHit hit;
              ray.origin = transform.position;
              ray.direction = Vector3.up;
              if(!Physics.Raycast(ray, out hit, 1)){
                  t_mesh.localScale = new Vector3(1, 1, 1);
                  ccr_controller.height = 1.8f;
                  Debug.Log("c_func 0");
              } else {
                  Debug.Log("Not enough space to stand up!");
              }
          }
      }
 }

1 Answer

1

If ive read this correctly, change the value of ControllerHeight to achieve your desired crouching level.

Java is very similar to C# in terms of syntax, a lot of what you know can be carried over.

Don’t be afraid to follow your instincts friend!

Hope this helps :slight_smile: