x


crouch script

hi, i am making a game and have no idea what im doing wrong here, i am trying to make a script to crouch the character (fps), im using the physicswalker and have a spere with a colider on the grond that is "the feet" that is a child of the player so what i want to do is to have a button that makes the distance between the sphere and the player have a lower value, when i push the button, the position doesnt decrease, here is my script (the value is right if i dont have the if(Input.GetKey("r")) in the script, but then it stays like that all time obviously)

function Start () { if(Input.GetKey("r")) { transform.localPosition = Vector3(0, -0.5, 0); } }

what am i doing wrong?

more ▼

asked Apr 10 '12 at 03:39 PM

victorlarsson gravatar image

victorlarsson
23 3 4 7

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Start() is only called once (the first time the object is activated, according to the documentation).

You should do this in Update() which is called every frame. Something like:

function Update()
{
    if(Input.GetKey("r"))
    {
        transform.localPosition = Vector3(0, -0.5, 0);
    }
    else
    {
        transform.localPosition = Vector3(0, 0, 0);
    }
}
more ▼

answered Apr 10 '12 at 03:59 PM

Kryptos gravatar image

Kryptos
7.2k 5 33

thank you very much, i still have a problem though, it worked to go down but when the char were about to rise again the collider teleported through the floor, i guess i would have to make it rise slow but have no idea how to do that, any ideas?

Apr 10 '12 at 06:37 PM victorlarsson
(comments are locked)
10|3000 characters needed characters left

I made this that will solve your problem, it scales your fps controller so that it looks like you're crouching..no problems with me so far..:)

  1. var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;

    function Start () { Screen.showCursor = false; }

    function Update () {

    if (tongle == 0){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.localScale += Vector3(0.0,-1,0); tongle = 1;
    } return; }

    if (tongle == 1){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.position.y += 0.5;
    transform.localScale += Vector3(0.0,1,0); tongle = 0;
    } return; } }

more ▼

answered Aug 04 '12 at 12:03 PM

Skotous gravatar image

Skotous
1

(comments are locked)
10|3000 characters needed characters left

I made this that will solve your problem, it scales your fps controller so that it looks like you're crouching..no problems with me so far..:)

  1. var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;

    function Start () { Screen.showCursor = false; }

    function Update () {

    if (tongle == 0){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.localScale += Vector3(0.0,-1,0); tongle = 1;
    } return; }

    if (tongle == 1){ if (Input.GetKeyDown (KeyCode.LeftControl)) {
    transform.position.y += 0.5;
    transform.localScale += Vector3(0.0,1,0); tongle = 0;
    } return; } }

more ▼

answered Aug 04 '12 at 12:03 PM

Skotous gravatar image

Skotous
1

(comments are locked)
10|3000 characters needed characters left

I made this that will solve your problem, it scales your fps controller so that it looks like you're crouching..no problems with me so far..:)

var tongle = 0; var localScale : Vector3; //var JumpSpeed = 100.0f;

function Start () {
Screen.showCursor = false; }





function Update () {

if (tongle == 0){ if
(Input.GetKeyDown
(KeyCode.LeftControl)) {    
transform.localScale +=
Vector3(0.0,-1,0);    tongle = 1;   
}    return; }


if (tongle == 1){ if
(Input.GetKeyDown
(KeyCode.LeftControl)) {   
transform.position.y += 0.5;   
transform.localScale +=
Vector3(0.0,1,0);    tongle = 0;   
}    return; }

}

P.S. sorry if i spammed that i wasn't sure if i did post it right

more ▼

answered Aug 04 '12 at 12:02 PM

Skotous gravatar image

Skotous
1

@Skotous this is my script and I too am having the falling through the floor issue after releasing my crouch button "c" I am unable to use your script. I get all kinds of errors. I am using the CharacterMotor.js and FPSInputController.js also.

function Update () {

if (Input.GetKeyDown ("c")) { transform.position.y -= 0.5;

  transform.localScale.y = 0.25;

}

if (Input.GetKeyUp ("c")) {

  transform.position.y += .7;

  transform.localScale.y = 2.0;

} }

Sep 03 '12 at 06:32 AM BluEye
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3419
x915
x600
x42

asked: Apr 10 '12 at 03:39 PM

Seen: 1292 times

Last Updated: Sep 03 '12 at 06:32 AM