I just started learning C# and general game development and at the moment i try to make a movement script for the player. I could use Rigidbody but i would like to handle some of the stuff my way.
Everything works fine… except… the jump. I always try to find and fix errors by myself but sadly i can’t find the bug here. The jump is like a teleport instead of a smooth jump… but tbh… i am not sure if this version of the script works. I tried so many ideas… and i also looked at so many examples i don’t get it and the frustration is kicking in very hard right now. I would love to get a little hint from someone. A sweet little hint that pushes me towards the right direction.
Your problem is primarily from line 50 where you create a fresh move variable each frame.
This wipes out any vertical velocity you might have had going from a jump.
One way to refactor might be to make move be a class-level variable, then ONLY set the X and Z components of it and leave the Y alone so that over time it can send you up and then gravity brings you back down.
Don’t worry though because even the CharacterController example that Unity provides in their example code is now unfortunately broken, but I was able to fix it all here, and you’re welcome to take a peek or else just use it yourself:
CharacterController CharMover broken:
I wrote about this before: the Unity example code in the API no longer jumps reliably. I have reported it. Here is a work-around:
One easy way to refactor this, after you have made the move variable be a class-level variable, is to do this:
// this is basically your code above assigned to a different variable
Vector3 lateralMove = (transform.forward * inputVertical + transform.right * inputHorizontal) * (moveSpeed / 8.0f);
// now copy only the X and Z parts to the move variable
move.x = lateralMove.x;
move.z = lateralMove.z;
That’s a cheap and cheerful way to leave the Y unmolested by your frame-to-frame control inputs, allowing JUMP to send it upwards, and allowing gravity to pull it down.
Awesome! And Merry Christmas. Also, I recommend you put open/close braces into the if/else statements starting from line 52 to line 58… while it is technically legal to have single-line actions resulting from if/else constructs, it’s safer to put curly braces around the actions to remind you precisely what code the if is controlling.
I actually do it in java… all the time… i mean the brackets. I only don’t use the brackets if i am 100% certain that i cant mess that part up Thanks for the advice tho.