Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Avoid a mesh problem!:(

Discussion in 'Scripting' started by fernandovt, Apr 5, 2014.

  1. fernandovt

    fernandovt

    Joined:
    Oct 1, 2012
    Posts:
    67
    Hello everyone! I have been developing a first person game and i have a problem with a script that i can't fix:(. The thing is that i have a script that compresses my character (to simulate crouching) and in this way the player can walk under things. My problem is that i have created a sword using objects of the game (cubes, cilinders, ect) and the sword is attached to the player, to be always in front of the camera. I need that this sword doesn't get affected by this script otherwise it will compress like the player and will look small each time i crouch. It's tricky but only for me i think because i'm terrible at scripting.

    I want to add something on it to avoid the meshes of the objects in my sword (This objects are in an empty object that i called "Sword"; Here is the Script:

    Code (csharp):
    1.  
    2. var crouchSpeed : float = 3; // crouching speed
    3.  
    4.  
    5.     private var isCrouching : boolean = false;
    6.     private var isInCrouchArea : boolean = false;
    7.  
    8.  
    9.     function Start ()  
    10.  
    11.     {
    12.  
    13.     isCrouching = false;
    14.  
    15.     }
    16.  
    17.     function Update ()
    18.  
    19.   {  
    20.  
    21.     if (Input.GetKeyDown ("c"))
    22.  
    23.     {  
    24.  
    25.     if(!isCrouching){
    26.  
    27.     isCrouching = true;
    28.     speed = crouchSpeed;
    29.     transform.position.y -= 3.5;      
    30.     transform.localScale.y = 1;
    31.  
    32.     }    
    33.  
    34.     else if (!isInCrouchArea){
    35.  
    36.     isCrouching = false;
    37.     transform.position.y += 5.5;            
    38.     transform.localScale.y = 4.5;
    39.  
    40.      
    41.           }
    42.        }
    43.     }
    44.  
    45.     function OnTriggerStay( other : Collider )
    46.  
    47.     {
    48.  
    49.         if ( other.gameObject.tag == "crouchArea"  )
    50.  
    51.         {
    52.  
    53.            if ( !isInCrouchArea )
    54.  
    55.            {
    56.  
    57.         isInCrouchArea = true;
    58.  
    59.         }
    60.       }
    61.     }
    62.  
    63.     function OnTriggerExit( other : Collider )
    64.     {
    65.  
    66.         if ( other.gameObject.tag == "crouchArea" )
    67.  
    68.         {
    69.  
    70.     isInCrouchArea = false;  
    71.  
    72.    if(isCrouching){
    73.  
    74.    isCrouching = false;
    75.     transform.position.y += 5.5;            
    76.     transform.localScale.y = 4.5;
    77.  
    78.  
    79.            }
    80.        }
    81.     }
    82.  
     
    Last edited: Apr 5, 2014