- Home /
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()
}
Answer by iwaldrop · Apr 19, 2013 at 05:41 AM
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. ;)
Sorry for the late reply, Unity Answers said they had removed this question. I've tried implenting the code you posted into $$anonymous$$e but I'm getting errors about missing semicolons when they are there.
$$anonymous$$y code is fine, the errors are with the code you posted. Unity says that it expects ; but it isn't there even though there is and the same with ). I've gone to each line and replaced them, tried adding extras too but still errors.
Well, if you double-click the log entry it should take you to the line that it's complaining about. I just manufactured a similar error and this is what it says:
Assets/Scripts/Generic/StrobeObject.cs(26,17): error CS1525: Unexpected symbol }', expecting
;'
That means that the compiler expects a semi-colon at Line 26, position 17. What does your error message say? Can you post it?
Answer by s_guy · Apr 20, 2013 at 05:20 AM
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".
Your answer
Follow this Question
Related Questions
Minor Addition to Jumping using CharaterController 1 Answer
Why can't my character jump 2 Answers
Using a touch event for character motor 0 Answers
Let Character Controller jump. 1 Answer
Jump Script ow to make it??? 2 Answers