Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Wesley21spelde · Jan 07 at 05:04 PM · triggerplayercrouching

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

226 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges