Skip to main content
Listed changes to original code. In short, this was a shot in the dark because the original question was retarded. What this hopes to do, is allows the player to control the character either by clicking a keyboard key, or by pressing a gui element. hurppity derp derp.
Source Link
return true
  • 942
  • 5
  • 18

Not sure if this is what you are asking or not, but this is a shot in the dark.

 #pragma strict 
 private var tr: Transform; 
 private var dist: float; // distance to ground

function Start(){
   tr = transform;  
   var ch:CharacterController = GetComponent(CharacterController);
   dist = ch.height/2; // calculate distance to ground
}

function Update()
{
  var vScale = 1.0;
  if (Input.GetKey("s")){ 
     vScale = 0.5;
  }

/I ADDED THIS BIT*************

  //What this does is checks each time Update() is called if the 'c' key was hit.
  //If you are pressing athe crouch key, then callrun the crouch function.
  if(Input.GetKeyUp("c")){
     crouch();
  }
 

/END OF EDIT*****************

  var ultScale = tr.localScale.y;  
  tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
  tr.position.y += dist * (tr.localScale.y-ultScale); 
}

function OnGUI ()
{
  if(GUI.Button(new Rect(15, 330, 200, 100), "Shrink"))
  { 

/* I ADDED THIS BIT*******************************

        //whenWhen thisyou buttonpress isa pressedbutton callthe crouchplayer crouches.
        crouch();

/END OF EDIT************************************

  }
}  

/I ADDED THIS BIT***********************

//This is the function that is run when you either press 'C' or press a gui button.

function crouch(){
    //make character crouch here
    //this is run once when the "Shrink Button" is pressed.
    //this is also done once when the c button is pressed.
    //Right not this only logs that the player is crouching. Additional logic will be needed for your character to actually crouch.
    Debug.Log("Crouch Button Pressed!");
} 

/END OF EDIT****************************

Not sure if this is what you are asking or not, but this is a shot in the dark.

 #pragma strict 
 private var tr: Transform; 
 private var dist: float; // distance to ground

function Start(){
   tr = transform;  
   var ch:CharacterController = GetComponent(CharacterController);
   dist = ch.height/2; // calculate distance to ground
}

function Update()
{
  var vScale = 1.0;
  if (Input.GetKey("s")){ 
     vScale = 0.5;
  }
  
  //If you are pressing a key, then call crouch
  if(Input.GetKeyUp("c")){
     crouch();
  }
 
  var ultScale = tr.localScale.y;  
  tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
  tr.position.y += dist * (tr.localScale.y-ultScale); 
}

function OnGUI ()
{
  if(GUI.Button(new Rect(15, 330, 200, 100), "Shrink"))
  { 
        //when this button is pressed call crouch.
        crouch();
  }
}  

function crouch(){
    //make character crouch here
    //this is run once when the "Shrink Button" is pressed.
    //this is also done once when the c button is pressed.
    Debug.Log("Crouch Button Pressed!");
} 

Not sure if this is what you are asking or not, but this is a shot in the dark.

 #pragma strict 
 private var tr: Transform; 
 private var dist: float; // distance to ground

function Start(){
   tr = transform;  
   var ch:CharacterController = GetComponent(CharacterController);
   dist = ch.height/2; // calculate distance to ground
}

function Update()
{
  var vScale = 1.0;
  if (Input.GetKey("s")){ 
     vScale = 0.5;
  }

/I ADDED THIS BIT*************

  //What this does is checks each time Update() is called if the 'c' key was hit.
  //If you are pressing the crouch key, then run the crouch function.
  if(Input.GetKeyUp("c")){
     crouch();
  }

/END OF EDIT*****************

  var ultScale = tr.localScale.y;  
  tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
  tr.position.y += dist * (tr.localScale.y-ultScale); 
}

function OnGUI ()
{
  if(GUI.Button(new Rect(15, 330, 200, 100), "Shrink"))
  { 

/* I ADDED THIS BIT*******************************

        //When you press a button the player crouches.
        crouch();

/END OF EDIT************************************

  }
}  

/I ADDED THIS BIT***********************

//This is the function that is run when you either press 'C' or press a gui button.

function crouch(){
    //make character crouch here
    //this is run once when the "Shrink Button" is pressed.
    //this is also done once when the c button is pressed.
    //Right not this only logs that the player is crouching. Additional logic will be needed for your character to actually crouch.
    Debug.Log("Crouch Button Pressed!");
}

/END OF EDIT****************************

Source Link
return true
  • 942
  • 5
  • 18

Not sure if this is what you are asking or not, but this is a shot in the dark.

 #pragma strict 
 private var tr: Transform; 
 private var dist: float; // distance to ground

function Start(){
   tr = transform;  
   var ch:CharacterController = GetComponent(CharacterController);
   dist = ch.height/2; // calculate distance to ground
}

function Update()
{
  var vScale = 1.0;
  if (Input.GetKey("s")){ 
     vScale = 0.5;
  }
  
  //If you are pressing a key, then call crouch
  if(Input.GetKeyUp("c")){
     crouch();
  }

  var ultScale = tr.localScale.y;  
  tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
  tr.position.y += dist * (tr.localScale.y-ultScale); 
}

function OnGUI ()
{
  if(GUI.Button(new Rect(15, 330, 200, 100), "Shrink"))
  { 
        //when this button is pressed call crouch.
        crouch();
  }
}  

function crouch(){
    //make character crouch here
    //this is run once when the "Shrink Button" is pressed.
    //this is also done once when the c button is pressed.
    Debug.Log("Crouch Button Pressed!");
}