- Home /
standard fps walking script makes me faster when jumping
Hi!
When I'm jumping with this script, it makes me go faster forward when I'm jumping, is there a way to avoid this?
var Walkspeed = 6.0;
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var cameraObject :GameObject;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function Update() {
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(look).currentYRotation, 0);
speed = Walkspeed;
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
Why do you say it makes you faster?
Your sideways x/z speed should be exactly the same in the air as the ground, which is easy to program, wrong according to real-life, but correct for video games (when you run, you have to make little jumps so you don't get stuck on the terrain, which don't affect your speed.)
Is your terrain really bumpy -- the would slow down ground speed, as it should.
Are you printing rigidbody.velocity? That will be faster, but only because of y. The "flat" x/z part is still the same.
im with him, your not, i was going to ask myself how the hell you knew.
well the problem is my "terrain" is a flat ground... so this is a bit tricky... btw. my values are 6 / 4 / 5 / 14
what values are?
Basically were pretty positive your not actually speeding up.
if your using velocity to deter$$anonymous$$e that. for example if that 6/4/5/14 is 4 different velocities thats ok.
velocity is the sum of your speed in 3 axes.
if you start out going 1 mph in the x direction and 1 mph in they z direction. and you jump and your going 2 mph in the y direction. Your velocity might be 4 but forward movement is only 2 still.
if you'd like to know your speed with regard to forward motion print out velocity.x and velocity.z
Your answer
Follow this Question
Related Questions
FPS camera wiggle as character walks 1 Answer
Make object visible only for camera "X" 1 Answer
Make so that a player cant see the players model? 1 Answer
DontDestroyOnLoad() Unity 3D 0 Answers
Network fps swap cameras 1 Answer