Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I tried many buttons but I can't connect them with the (Java)Script.

So I made 2 gui buttons: one for jump which was easy because I have an if statement and if it's true then jump.

But I have problems with the crouch button. Bellow is my script but I can't figure out how I can do this from Update in function Gui.

(Or if you know how to connect buttons with JS functions, please tell me: it's always a better option :D )

This is my script:

#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;
  }

  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"))
  { 
  }
}   
share|improve this question
    
#pragma strict? This is for Unity, right? – Anko Oct 21 at 21:15
    
Yes it is i cant figure out how to put the crouch code in function OnGui – Nikola Pejovski Oct 21 at 22:15
    
I will save you some headache online --- people are rather touchy when it comes to Java / Javascript. In unity it uses Javascript, and modified version at that, so people usually call it Unityscript. Java is absolutely different than Javascript (Java is actually the spiritual father to C# --- which is also used in Unity), and getting Java and Javascript confused is a programming sin online. – return true Oct 22 at 3:09
    
@Anko '#pragma strict' is required for unity when using js. – return true Oct 22 at 3:25

1 Answer 1

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!");
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.