From 9c10ca695dbeaa5d0e785a54a6d3e5bbe9653528 Mon Sep 17 00:00:00 2001 From: Gregory Campbell Date: Thu, 5 Dec 2019 22:42:16 -0500 Subject: [PATCH] Crouching/crawling functionality is working --- Assets/Scripts/PlayerController.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index b692186..d61dfd5 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -10,6 +10,7 @@ using System.Collections; public class PlayerController : MonoBehaviour { CharacterController characterController; + CapsuleCollider capsule; public float speed; public float jumpHeight; @@ -19,11 +20,18 @@ public class PlayerController : MonoBehaviour public GameObject playerModel; private int jump = 0; + private float capsuleHeight; + private float controllerHeight; + private float transformHeight; private Vector3 moveDirection; void Start() { characterController = GetComponent(); + capsule = GetComponent(); + transformHeight = transform.localScale.y; + controllerHeight = characterController.height; + capsuleHeight = capsule.height; } void Update() @@ -55,6 +63,19 @@ public class PlayerController : MonoBehaviour moveDirection = moveDirection.normalized * speed; } + if(Input.GetKey(KeyCode.LeftControl)) + { + moveDirection = (moveDirection.normalized * speed/4); + characterController.height /= 2; + capsule.height /= 2; + transform.localScale = new Vector3(transform.localScale.x, transformHeight/2, transform.localScale.z); + } else { + moveDirection = moveDirection.normalized * speed; + characterController.height = controllerHeight; + capsule.height = capsuleHeight; + transform.localScale = new Vector3(transform.localScale.x, transformHeight, transform.localScale.z); + } + } if (Input.GetButtonDown("Jump") && jump <= 1)