I’ve coded a basic movement system that lets a cube walk around, sprint and jump. I’d like to have it though so if left alt is on then the cube goes into a hover state and is able to “fly” around with the WSAD moving the character and space increasing the Y and ctrl decreasing the Y.
I’m not asking that someone writes the code for me but if you could point me in the right direction that would be fantastic. I’m quite new to the character coding and I’ve been doing a lot of the tutorials but I can’t seem to figure this out.
I basically just want it so that when lAlt is true the cube slowly ascends and stays at a set Y position and then when lAlt is false it descends back down.
My code is:
// Player Variables
var speed = 7.0;
var rotateSpeed = 3.0;
//Jumping Variables
var moveDirection : Vector3 = Vector3.zero;
var gravity = 0.3;
var jumpHeight = 15;
var lAlt = false;
function Update ()
{
//Basic Movement
var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
controller.SimpleMove(forward * curSpeed);
//Left Shift to boost speed by 9
if(Input.GetKey(KeyCode.LeftShift))
{
speed = 15;
}
else
{
speed = 6;
}
//Jumping input
if(Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpHeight;
}
moveDirection.y -= gravity;
controller.Move(moveDirection * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftAlt))
{
if(lAlt == true)
{
//HoverCode
lAlt = false;
}
else
{
//Stop Hovering
lAlt = true;
}
}
//End of Update()
}