x


accesing character controler and change height

well as you see I want this... when pressing Ctrl make height=2 > height=1 ( Crouch) I used get component but I don't know what to do cause it's not script.... thnx!

more ▼

asked Aug 15 '11 at 08:04 PM

MobinS gravatar image

MobinS
56 24 32 36

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

3 answers: sort voted first

thnx man ... I used this : if (Input.GetKeyDown(KeyCode.LeftControl)) { if (controller.height == 2) controller.height = 1.8f; else controller.height = 2; } if (Input.GetKeyUp(KeyCode.LeftControl)) { if (controller.height == 1.8f) controller.height = 2; else controller.height = 1.8f;

... but there is another problem... when I crouch then get Up ... the collider will get over terrain so the camera goes under terrain and fall... IDK what should I do!

more ▼

answered Aug 15 '11 at 09:09 PM

MobinS gravatar image

MobinS
56 24 32 36

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

Not sure what you meant by using get component but you are able to access the character controller through getComponent

try CharacterController controller = GetComponent();

if(Input.GetKeyDown(KeyCode.LeftControl)) { controller.height = 1; }

more ▼

answered Aug 15 '11 at 08:13 PM

illwunn gravatar image

illwunn
61 2

it was supposed to be GetComponent();

Aug 15 '11 at 08:14 PM illwunn

I didn't got it ... I put "if(Input.GetKeyDown(KeyCode.LeftControl)) { controller.height = 1; }" in Update... so I should tell what is "controller"... I add "CharacterController controller = GetComponent(); " up in public field... I used C#... but it give error that getcomponent is not right...

Aug 15 '11 at 08:23 PM MobinS
(comments are locked)
10|3000 characters needed characters left

ok i constructed a cube and attached a character controller to it with a height of 2

and then i attached a script called character that adjusts theheight when left ctrl is pressed the code was:

public class Character : MonoBehaviour {

CharacterController controller;

// Use this for initialization
void Start () {
    controller = GetComponent<CharacterController>();
}

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown(KeyCode.LeftControl))
    {
        if (controller.height == 2)
            controller.height = 1;
        else
            controller.height = 2;
    }
}
}
more ▼

answered Aug 15 '11 at 08:31 PM

illwunn gravatar image

illwunn
61 2

(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:

x42

asked: Aug 15 '11 at 08:04 PM

Seen: 676 times

Last Updated: Aug 15 '11 at 09:09 PM