2
\$\begingroup\$

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"))
  { 
  }
}   
\$\endgroup\$
5
  • \$\begingroup\$ #pragma strict? This is for Unity, right? \$\endgroup\$
    – Anko
    Oct 21, 2015 at 21:15
  • \$\begingroup\$ Yes it is i cant figure out how to put the crouch code in function OnGui \$\endgroup\$ Oct 21, 2015 at 22:15
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 22, 2015 at 3:09
  • \$\begingroup\$ @Anko '#pragma strict' is required for unity when using js. \$\endgroup\$ Oct 22, 2015 at 3:25
  • \$\begingroup\$ The problem is here: 5*Time.deltaTime. This value never approaches 1, so the character is never fully crouched. \$\endgroup\$ Dec 21, 2015 at 15:22

2 Answers 2

1
\$\begingroup\$

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*****************************

\$\endgroup\$
2
  • \$\begingroup\$ "Give a man a fish and you feed him for a day; teach him fishing and you feed him for a lifetime" please, in your answer show what you changed and why. \$\endgroup\$
    – Bálint
    Oct 5, 2016 at 7:48
  • \$\begingroup\$ Changes noted. You're welcome. \$\endgroup\$ Oct 5, 2016 at 8:01
0
\$\begingroup\$

Try doing this (including only the neccessary changes, not the whole script)

private var crouchTime:float = 0;
private var vScale:float = 1.0f;
private var prevScale:float = 1.0f;

function Update() {
    //if this frame the key down state was initiated (true once)
    if (Input.GetKeyDown("s")){ 
        crouchTime = 0.0f;
        vScale = 0.5f;
        prevScale = tr.localScale.y;
    }
    //if this frame the key up state was initiated (true once)
    if (Input.GetKeyUp("s")){ 
        crouchTime = 0.0f;
        vScale = 1.0f;
        prevScale = tr.localScale.y;
    }
    //we only need to increment up to 1
    if(crouchTime < 1) {
        crouchTime += 5 * Time.deltaTime;
    }

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

This creates a steady, linear transition from "crouched" to "not crouched" over the course of 0.2 seconds, as we save the starting position and the ending position as separate values, as well as how long the button has been held down (or released).

In order to prevent un-crouching while underneath an object that should keep the character crouched, you would need a separate input, eg.:

if (Input.GetKeyUp("s") && !mustRemainCrouched()){ }

Where mustRemainCrouched() is a method that checks to see if there is room to stand up (Physics.CapsuleCast() will come in handy writing this function). Be aware that this sample check is still incomplete, as if the player releases 's' and then moves into the open, they will not stand up as the key was not released that frame. From this, however, you should have all the information you need to construct your own check that will do what you need.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .