moveDirection.y = jumpSpeed;
SETS the vertical component of the movement to the jump value.
Now computers don't read minds (yet), so something's gotta set it back to whatever it was before.
moveDirection.y -= gravity * Time.deltaTime;
Decreases it every frame.
What you probably are looking for is
//This is frame 1
if move event occured ==> Increase
horizontal movement component of the
vector by groundSpeed amount (Increase, not override)
if jump event occured ==> Increase
vertical movement component of the
vector (Increase, not override)
if gravity event occured ==> Decrease
vertical movement component of the
vector (Decrease, not override)
//End of the update of this frame
What you are doing now is
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
You assigned the value of jumpSpeed. If you are falling at 200 km/h and you would say, well I just jumped, your speed would not be -200 km/h but +jumpSpeed , instead of the result of the sum: "-200km/h + jumpSpeed" , assuming you could jump in the air somehow of course.
Further more as you may have yet noticed, I haven't really answered your question, and actually you may have just wanted something along the lines of "please fix my script", I do not know (No offense meant). I simply felt the need to help you understand the question first. If that makes any sense.
EDIT 12/22/2010 08:49 GMT+1
Clarification on vote down:
I think the question was lacking in quality due to primarily the fact that it ends with a "why is this occuring" when a complete copy pasted occured of documented code. It's probably the hello world script of Unity3D. Now that says to me that the user is probably just trying to get one effect no matter how. So it's becoming a "please explain how to program question" OR a "just fix it" question. Both I really dislike since the only person that will learn anything from this Q&A will be the one asking the question. I feel it's a good question if several other users may encounter it and use it, by reference or google search or any other means and not become a dead end.
Also I do not feel it is correct to state:
When using this script from the docs
for moving character controllers, my
character lifts off the ground. Why?
Without stating what the expected behaviour is
From the comments on my post it looks more like double input registration than anything else. But this question is asking why there is vertical movement.
P.S.
Well the gravity is upside down. It should be "someObject" += Physics.gravity.y or another value. Why? Because gravity is a negative value and double negative is positive. So
( X -= -9.81 ---> X += 9.81 ) Exactly the same.
Here's a test sample included as a Free-B
void Update()
{
if (GravityOn)
{
moveDirection = new Vector3(moveDirection.x, moveDirection.y + Physics.gravity.y, moveDirection.z);
}
_controller.Move(moveDirection);
moveDirection = Vector3.zero;
}
Mind you that this is NOT affected by scaling. Huge objects will SEEM to fall slower, but that happens in the real world aswell. Similarly you can say that you may not want to use gravity with -9.81 all the time it can seem pretty fast at times. Also be carefull when jumping, you need to ADD gravity + jumpheight, otherwise you need to calculate how much effort you need to overcome gravity all the time, when in fact when you change global gravity you probably want to maintain the actual jumpheight (Most of the time, not like the UT low grav. mode)