- Home /
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?
Answer by Kryptos · Apr 10, 2012 at 03:59 PM
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);
}
}
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?
Answer by Skotous · Aug 04, 2012 at 12:03 PM
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; } }
Answer by Skotous · Aug 04, 2012 at 12:03 PM
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; } }
Answer by Skotous · Aug 04, 2012 at 12:02 PM
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
@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 Character$$anonymous$$otor.js and FPSInputController.js also.
function Update () {
if (Input.Get$$anonymous$$eyDown ("c")) { transform.position.y -= 0.5;
transform.localScale.y = 0.25;
}
if (Input.Get$$anonymous$$eyUp ("c")) {
transform.position.y += .7;
transform.localScale.y = 2.0;
} }
How do you fix it if you want the crouch to not work in certain areas (without use of triggers).
example. if youre inside an air vent. if you press the crouch button in areas like this, the character just pops out of the walls. How to fix that?
Your answer
Follow this Question
Related Questions
Vector3 not receiving coordinates 1 Answer
addition of vector3 1 Answer
Creating a bouncing game without using physics - Vector3 math problem, 2 Answers
How to keep the same position of the object instead of camera 0 Answers
Distance won't work 1 Answer