Croutch Player when Trigger enter
Hey guys i have a question Can someone help me i got this script from a tutorial but i am trying to get it also working when i enter a trigger so my player stays Crouched OntriggerEnter
And When i OntriggerExit my Player UnCrouches but i cant seem to get it working i tried multiple things i have the void AreWeCrouched this one is trigger by a trigger on my other object no problem but my player does not stay crouched i think i know where the problem is
static bool IsKeyPressed(KeyCode[] keys)
{
// Return true if any of the keys are down.
for (int i = 0; i < keys.Length; i++)
if (Input.GetKey(keys[i]))
return true;
return false;
}
}
but i dont know how to fix it can someone please help me i will post the script here
using System; using System.Collections; using UnityEngine;
public class Crouch : MonoBehaviour
{
public bool isCrouchedTrigger = false;
public bool isWeAreCrouche = false;
public float crouchYLocalPosition = 1;
public Transform head;
[HideInInspector]
public float defaultHeadYLocalPosition;
[Tooltip("Capsule collider to lower when we crouch.\nCan be empty.")]
public CapsuleCollider capsuleCollider;
public CharacterController cc;
[HideInInspector]
public float defaultCapsuleColliderHeight;
[SerializeField]
GroundCheck groundCheck;
public KeyCode[] keys = new KeyCode[] { KeyCode.LeftControl, KeyCode.RightControl };
public bool IsCrouched { get; private set; }
public event System.Action CrouchStart, CrouchEnd;
public bool isRunning = false;
//public Transform visibleCapsule;
// we are adding a value to the transform of the visibleCapsule normal value = 0.8591545
void Reset()
{
head = GetComponentInChildren<Camera>().transform;
capsuleCollider = GetComponentInChildren<CapsuleCollider>();
// Get or create the groundCheck object.
groundCheck = GetComponentInChildren<GroundCheck>();
if (!groundCheck)
groundCheck = GroundCheck.Create(transform);
}
void Start()
{
defaultHeadYLocalPosition = head.localPosition.y;
if (capsuleCollider)
defaultCapsuleColliderHeight = capsuleCollider.height;
}
public void AreWeCrouched()
{
isCrouchedTrigger = true;
if (isCrouchedTrigger = true)
{
// Enforce crouched y local position of the head.
head.localPosition = new Vector3(head.localPosition.x, crouchYLocalPosition, head.localPosition.z);
// Lower the capsule collider.
if (capsuleCollider)
{
capsuleCollider.height = defaultCapsuleColliderHeight - (defaultHeadYLocalPosition - crouchYLocalPosition);
capsuleCollider.center = Vector3.up * capsuleCollider.height * .5f;
capsuleCollider.height = 1.40f;
cc.height = 1.40f;
isCrouchedTrigger = true;
}
// Set state.
if (!IsCrouched)
{
IsCrouched = true;
CrouchStart?.Invoke();
}
isWeAreCrouche = true;
IsCrouched = true;
CrouchStart?.Invoke();
}
}
public void AreWeCrouchedFalse()
{
isWeAreCrouche = false;
if (isCrouchedTrigger = false)
{
IsCrouched = false;
}
}
[PunRPC]
void LateUpdate()
{
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{
isRunning = true;
}
else
{
isRunning = false;
if (isRunning == true){
}
else
{
if (IsKeyPressed(keys))
{
// Enforce crouched y local position of the head.
head.localPosition = new Vector3(head.localPosition.x, crouchYLocalPosition, head.localPosition.z);
// Lower the capsule collider.
if (capsuleCollider)
{
capsuleCollider.height = defaultCapsuleColliderHeight - (defaultHeadYLocalPosition - crouchYLocalPosition);
capsuleCollider.center = Vector3.up * capsuleCollider.height * .5f;
capsuleCollider.height = 1.40f;
cc.height = 1.40f;
isCrouchedTrigger = true;
}
// Set state.
if (!IsCrouched)
{
IsCrouched = true;
CrouchStart?.Invoke();
}
}
else if (IsCrouched = false)
{
// Reset the head to its default y local position.
head.localPosition = new Vector3(head.localPosition.x, defaultHeadYLocalPosition, head.localPosition.z);
// Reset the capsule collider's position.
if (capsuleCollider)
{
capsuleCollider.height = defaultCapsuleColliderHeight;
capsuleCollider.center = Vector3.up * capsuleCollider.height * .5f;
cc.height = 1.8f;
isCrouchedTrigger = false;
}
// Reset state.
IsCrouched = false;
CrouchEnd?.Invoke();
}
}
}
}
static bool IsKeyPressed(KeyCode[] keys)
{
// Return true if any of the keys are down.
for (int i = 0; i < keys.Length; i++)
if (Input.GetKey(keys[i]))
return true;
return false;
}
}
Thanks Guys...
Your answer
![](https://koobas.hobune.stream/wayback/20220613051407im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help on how to code crouching? 0 Answers
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers
How to activate "OnTriggerEnter" when Player ray hits an object collider? 1 Answer
My trigger won't activate the UI when the player collides with it. 0 Answers
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 1 Answer