- Home /
Question by
1eleven · Sep 04, 2013 at 06:47 PM ·
javascriptcharactercontrollerif-statementsnoob
flying should only be possible while fuel 0 < but it continues so long as space is held
i am trying to give my player the ability to fly and i have it setup so that when your fuel is more than 0 and space is held you can fly, but when you hold space you will continue to fly even after you run out of fuel, but if you let space up and try again it works as intended, i am probably missing something obvious
#pragma strict
private var x : float;
private var y : float;
private var z : float;
private var mouse : float;
private var move : Vector3;
private var pack = 0f;
private var grounded = true;
private var jumping = false;
var packmax = 100;
var mouseSpeed = 10;
var moveSpeed = 10;
var gravity = 5f;
var jumpheight = 5;
function Update()
{
var ground : CharacterController = GetComponent(CharacterController);
if(ground.isGrounded)
{
grounded = true;
}
if(ground.isGrounded == false)
{
grounded = false;
}
mouse = transform.eulerAngles.y += Input.GetAxis("Mouse X") * mouseSpeed;
z = Input.GetAxis("Vertical") * moveSpeed;
x = Input.GetAxis("Horizontal") * moveSpeed;
if(Input.GetKey(KeyCode.Space) == true)
{
jumping = true;
}
else
{
jumping = false;
}
if(pack > 0)
{
if(jumping == true)
{
y = jumpheight;
//Debug.Log("jumping");
}
}
if(jumping == false)
{
y = gravity;
//Debug.Log("not jumping");
}
move = transform.TransformDirection(x, y, z);
}
function FixedUpdate()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.eulerAngles = Vector3(0, mouse, 0);
controller.Move(move);
var ground : CharacterController = GetComponent(CharacterController);
if(jumping == true)
{
if(pack > 0)
{
pack --;
}
}
if(ground.isGrounded)
{
if(pack < 100)
{
pack ++;
}
}
//Debug.Log(pack);
}
i have some pretty shitty naming conventions when it come to naming variables and there is probably some that i am not even using, i will go back through and fix it once i have it functional
Comment
Answer by perchik · Sep 04, 2013 at 06:52 PM
I don't think you can do an else
statement on if(Input.GetKey())
but, what you could do is to add a statement that says
if(Input.GetKeyUp(KeyCode.Space) jumping = false;
which says that when you release the spacebar, jump is set back to false