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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Collision between Character Controller and Box Collider not working.

Discussion in 'Scripting' started by Vrakko, Aug 22, 2013.

  1. Vrakko

    Vrakko

    Joined:
    Mar 13, 2013
    Posts:
    2
    I have recently started a small project in Unity and have been having problems with the collision between the two components mentioned above.
    I have an object with a Character Controller attached working as my character and a column, which is supposed to move the character who is standing on top of it, which has a Box Collider. When the column moves downwards everything works as intended, but when moving upwards the character falls through the column as if the the collision wasn't working, even if it collides with it when it isn't moving.

    These are the scripts that I am using:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.        
    6.     //Variables
    7.        
    8.     public float speedx = 6.0F;
    9.     public float jumpSpeed = 8.0F;
    10.     public float gravity = 20.0F;
    11.         private Vector3 rotation;
    12.     private Vector3 moveDirection = Vector3.zero;
    13.  
    14.     void Update() {
    15.                
    16.         CharacterController controller = GetComponent<CharacterController>();
    17.                
    18.         if (controller.isGrounded)
    19.                 {            
    20.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    21.             moveDirection = GameObject.Find("PJCamera").transform.TransformDirection(moveDirection);  
    22.                        
    23.                         if(moveDirection.y != 0)
    24.                         {
    25.                                 moveDirection.y = 0;
    26.                         }
    27.                        
    28.             moveDirection *= speedx;
    29.                        
    30.             if (Input.GetButton("Jump"))
    31.                         {
    32.                 moveDirection.y = jumpSpeed;
    33.                         }
    34.                        
    35.         }
    36.                
    37.         //Applying gravity to the controller
    38.         moveDirection.y -= gravity * Time.deltaTime;
    39.                
    40.         //Making the character move
    41.         controller.Move(moveDirection * Time.deltaTime);
    42.                
    43.                 //transform.rotation=new Quaternion(0, GameObject.Find("Main Camera").transform.rotation.y,0,0);
    44.                 transform.eulerAngles = new Vector3 (0,GameObject.Find("PJCamera").transform.eulerAngles.y,0);
    45.                
    46.                
    47.                
    48.     }
    49.        
    50.        
    51.        
    52. }


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ColMovement : MonoBehaviour {
    6.  
    7.         // Use this for initialization
    8.        
    9.         private bool activated ;
    10.         public float higherLimit;
    11.         public float lowerLimit;
    12.         public float startingpoint;
    13.         public int speed;
    14.         public string lastMovement;
    15.         private bool bajar , subir ;
    16.         public string levername ;      
    17.         public KeyCode kC;
    18.         public bool PJHit;
    19.        
    20.  
    21.        
    22.         void Start () {
    23.        
    24.                 activated = false;
    25.                
    26.                 speed = 1 ;
    27.                 bajar = false;
    28.                 subir = false;
    29.                 lastMovement = "still" ;
    30.                 PJHit = false ;
    31.                
    32.                
    33.         }
    34.        
    35.         // Update is called once per frame
    36.         void Update () {              
    37.                
    38.                 ActivationToggle();
    39.                 columnMovement();              
    40.                
    41.         }
    42.        
    43.         void ActivationToggle ()
    44.         {
    45.                
    46.                                
    47.                
    48.                 if( Input.GetKeyDown(kC) )
    49.                 {
    50.                 activated = !activated ;                      
    51.                 }
    52.                
    53.          
    54.                
    55.                
    56.         }
    57.        
    58.         void columnMovement()
    59.         {
    60.                
    61.                
    62.                 Vector3 actualPosition = transform.position;
    63.        
    64.                 if( !(GameObject.Find(levername).animation.isPlaying)  lastMovement!="still" )
    65.                 {
    66.                         if(lastMovement=="bajar")
    67.                         {
    68.                                 bajar = true ;
    69.                         }
    70.                         else
    71.                         {
    72.                                 subir = true ;
    73.                         }
    74.                         lastMovement="still";
    75.                 }
    76.                
    77.                 if(subir == false  bajar == false  activated  !(GameObject.Find(levername).animation.isPlaying) )
    78.                 {
    79.                         if (Input.GetKeyDown(KeyCode.LeftArrow))
    80.                         {              
    81.                                 if (actualPosition.y == startingpoint)
    82.                                 {
    83.                                         GameObject.Find(levername).animation.Play("MiddleToLeft");
    84.                                         GameObject.Find(levername).animation["MiddleToLeft"].speed = 1;
    85.                                        
    86.                                 }
    87.                                 else if (actualPosition.y > startingpoint)
    88.                                 {
    89.                                         GameObject.Find(levername).animation.Play("RightToMiddle");
    90.                                         GameObject.Find(levername).animation["RightToMiddle"].speed = 1;
    91.                                
    92.                                 }
    93.                                
    94.                                 lastMovement = "bajar";
    95.                                
    96.                         }
    97.                         else if (Input.GetKeyDown(KeyCode.RightArrow))
    98.                         {
    99.                                 if (actualPosition.y == startingpoint)
    100.                                 {
    101.                                         GameObject.Find(levername).animation.Play("RightToMiddle");
    102.                                         GameObject.Find(levername).animation["RightToMiddle"].time = GameObject.Find("Lever").animation["RightToMiddle"].length ;
    103.                                         GameObject.Find(levername).animation["RightToMiddle"].speed = -1;                                      
    104.                                        
    105.                                 }
    106.                                 else if (actualPosition.y < startingpoint)
    107.                                 {
    108.                                         GameObject.Find(levername).animation.Play("MiddleToLeft");
    109.                                         GameObject.Find(levername).animation["MiddleToLeft"].time = GameObject.Find("Lever").animation["MiddleToLeft"].length ;
    110.                                         GameObject.Find(levername).animation["MiddleToLeft"].speed = -1;                              
    111.                                 }
    112.                                 lastMovement = "subir";
    113.                                
    114.                         }
    115.                 }      
    116.                
    117.                 if ( subir  actualPosition.y < higherLimit)
    118.                 {
    119.                         if ( actualPosition.y + (speed*Time.deltaTime) > higherLimit )  
    120.                         {
    121.                                 actualPosition.y = higherLimit ;
    122.                         }      
    123.                         else if ( (actualPosition.y != startingpoint)  (actualPosition.y + (speed*Time.deltaTime) > startingpoint)  (actualPosition.y < startingpoint) )
    124.                         {
    125.                                 actualPosition.y = startingpoint;
    126.                         }
    127.                         else {
    128.                         actualPosition.y+=speed*Time.deltaTime;
    129.                         }
    130.                 }
    131.                 else if ( bajar  actualPosition.y > lowerLimit )
    132.                 {
    133.                         if (actualPosition.y - (speed*Time.deltaTime) < lowerLimit )
    134.                         {
    135.                                 actualPosition.y = lowerLimit;
    136.                         }                      
    137.                         else if ( (actualPosition.y != startingpoint)  (actualPosition.y - (speed*Time.deltaTime) < startingpoint)  (actualPosition.y > startingpoint))
    138.                         {
    139.                                 actualPosition.y = startingpoint;
    140.                         }
    141.                         else {
    142.                         actualPosition.y-=speed*Time.deltaTime;
    143.                         }
    144.                 }      
    145.                        
    146.                 if( PJHit  lastMovement == "subir" )
    147.                 {
    148.                        
    149.                 }
    150.                 transform.Translate(0,0,actualPosition.y-transform.position.y) ;      
    151.                
    152.                
    153.                 if (actualPosition.y == lowerLimit || actualPosition.y == higherLimit || actualPosition.y == startingpoint)
    154.                 {
    155.                         bajar = false ;
    156.                         subir = false;
    157.                 }
    158.                
    159.                
    160.                
    161.         }      
    162.        
    163.        
    164. }
    165.  
     
  2. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    Does the characterController have a Collider?
     
  3. Vrakko

    Vrakko

    Joined:
    Mar 13, 2013
    Posts:
    2
    Yes, a Capsule Collider.