Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to change height of Character Controller through onTriggerEnter()

Discussion in 'Scripting' started by keaton_burnett, Sep 27, 2020.

  1. keaton_burnett

    keaton_burnett

    Joined:
    Mar 31, 2019
    Posts:
    1
    I've been searching for a solution for this problem all day, but no luck. Basically what I have is a FirstPersonCharacter prefab (Unity Standard Assets), and a cube that has a box collider as a trigger. When the player enters the box, I want the height of the Character Component to decrease from 1.8 to 1. The goal is to make the character crouch when they enter an area. Here's what my code looks like right now

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class CrouchTrigger : MonoBehaviour {
    8.  
    9.     public GameObject Player;
    10.     public GameObject trigger;
    11.  
    12.     public bool crouched;
    13.  
    14.     public CharacterController CC;
    15.  
    16.     public Animation crouch;
    17.  
    18.    
    19.     void Start() {
    20.         CC = Player.GetComponent<CharacterController>();
    21.         crouch = Player.GetComponent<Animation>();
    22.        
    23.      
    24.  
    25.     }
    26.  
    27.  
    28.     void Update() {
    29.         if (crouched == true) {
    30.             CC.height = 1;
    31.         }
    32.     }
    33.  
    34.     void OnTriggerEnter() {
    35.        
    36.         crouch.Play();
    37.         crouched = true;
    38.  
    39.     }
    40.  
    41.    
    42. }
    43.  

    I should note that the animation does play, but any attempts to adjust the CharacterController height does not work. I can make it work by setting the function to a key, but I want it to happen as the player passes through the sphere. Any help would be greatly appreciated, thanks!
     
  2. Poooiu

    Poooiu

    Joined:
    Oct 4, 2015
    Posts:
    4
    I guess the sphere you mentioned is SphereCollider.
    There are methods that can help you with interacting with colliders. It is always good to dont have too many things at Update().

    Attach this script to GameObject with SphereCollider.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class sphere : MonoBehaviour
    4. {
    5.     public GameObject Player;
    6.     public CharacterController CC;
    7.  
    8.     public void Awake()
    9.     {
    10.         Player = GameObject.Find("Player");
    11.         CC = Player.GetComponent<CharacterController>();
    12.     }
    13.  
    14.     public void OnTriggerEnter(Collider other)
    15.     {
    16.         CC.height = 1f;
    17.     }
    18. }
    19.  
    Dont forget to mark "Is Trigger" at Inspector for Sphere Collider.
    In addition you may need to tweak
    Code (CSharp):
    1.  
    2. CC.center.y
    3.  
    To change center of CharacterController, since height is offseted equally up and down from center of CC.
    If sphere is as big as crouch area, you can add this to provided script to ensure that Character Controller will go back to 1.8f after leaving sphere.
    Code (CSharp):
    1.  
    2.     public void OnTriggerExit(Collider other)
    3.     {
    4.         CC.height = 1.8f;
    5.     }
    6.  
    If you used "CC.center.y", dont forget to implement the original value at OnTriggerExit.

    NOTE: Any collider that will enter the sphere will trigger the effect, so it is just most simple solution for your problem and you may need to play with it to carve it to your needs, but it should give you an idea how to achieve this kind of behaviour.

    If you would like this to trigger only if player character enters the sphere there is number of ways, but one of them can be.

    Code (CSharp):
    1.  
    2. public void OnTriggerEnter(Collider other)
    3. {
    4.     if (other.gameObject.name == "Player")
    5.     {
    6.        CC.height = 1f;
    7.     }
    8. }
    9.  
     
    Last edited: Sep 27, 2020