Hovering Cube on CapsLock?

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()
}

If you’re using a rigidbody just turn off the gravity. If you want the floaty effect when you first engage flight mode, then just sample the current y position and add a steadily decreasing value to it over the next second or so.

If you want to move up 1m over 2 seconds, then:

IEnumerator FloatEffect(float time, float distance)
{	
	rigidbody.useGravity = false;
	
    float startTime = Time.time;
    Vector3 startPosition = transform.position;
    Vector3 endPosition = startPosition + (Vector3.up * distance);

    while (startTime + time >= Time.time)
    {
		transform.position = Vector3.MoveTowards(transform.position, endPosition, distance * Time.deltaTime);
		yield return null;
    }
}

I’ll leave it up to you to figure out how to smooth it. :wink:

For smoothing actor movement, as with your intent to smooth hover, search for Lerp and SmoothBlend methods and investigate them. Search on here seems to suck (fail or never conclude), so use Google and search for things like “unity lerp” or “unity lerp movement”. Most of the relevant stuff will point back to this site. You might want to figure out these techniques in isolated studies before trying to apply them to your need.

You probably want a general gravity solution to handle your descent or hover turning off, and the object returning to ground. Search for “unity gravity character controller”.