3D Third Person Jump Movement code bug

I can’t figure out what’s causing my cube to fly off to the sky and outside of the frustum view.

var velocity_y = 0f;
        if(body.velocity.y < -speed+2f)
        {
            velocity_y = -speed+2f;
        }
        else
        {
             velocity_y = body.velocity.y;
        }

if (jump)
        {
            body.AddForce(0, 300, 0);
            jump = false;
        }
        else
        {
            body.velocity = (Quaternion.Euler(0, cam.transform.eulerAngles.y, 0) * new Vector3(h, velocity_y, v) * speed);

        }

Above code is in FixedUpdate. My jump bool is turned true in the Update when space is pressed with getkeydown.

This is what happens to my cube guy:

63ab0

It flies off to the sky forever! I’ve been trying all kinds of combinations. I tried doing the body.velocity = Vector3.up * speed thing and adding it to the body.velocity, still flies off. What’s causing it to fly off?

Well, it kind of looks like on line 19 there, you’re just increasing velocity every frame after you start having even a small amount of upward velocity. You’re basically multiplying the current velocity by speed, and assigning that as the new velocity. If you comment out that line, does the problem go away?

As a potentially bigger issue, it’s somewhat concerning to see you using both AddForce and manually setting velocity. It’s usually one or the other, except for very specific cases.

Thank you for your response! And yes you’re totally right! I forgot to mention that. But I did try to change velocity also by adding some speed to the Vector3.up.
When I remove line 19 it totally works but I want movement while jumping. But line 19 really works well on its own in that it has really smooth movement with a complete 180 degree rotation and everything. How do I include that movement along with jumping? I still don’t quite understand why it flies to infinity upwards because when im moving, velocity.y is zero.

It’s going to infinity, I believe, because every Fixed Update, it’s multiplying the velocity by the Speed. Even if the speed is not very large, the velocity will just keep growing faster and faster. For example, after you jump, let’s say your velocity is (0, 10, 0), because you’re moving up. And let’s say the Speed you’re using is 5. After the next fixed update, the velocity will be (0, 50, 0), then after the next FixedUpdate it will be (0, 250, 0), because you just keep multiplying by speed. Eventually the velocity will be infinite. You’re not seeing this as a problem when you’re on the ground because you’re multiplying the speed by 0, since vertical velocity is 0 in that case.

Anyway, I figure you can fix this by not adjusting the y component of the velocity on line 19. Right now you have this:

body.velocity = (Quaternion.Euler(0, cam.transform.eulerAngles.y, 0) * new Vector3(h, velocity_y, v) * speed);

Maybe something like this:

var tempVelocity = (Quaternion.Euler(0, cam.transform.eulerAngles.y, 0) * new Vector3(h, 0, v) * speed);
body.velocity = new Vector3(tempVelocity.x, velocity_y, tempVelocity.z );

I’m not 100% sure that works, but the idea would be to just pass through the y-component of the velocity, so that this code doesn’t affect it at all.

1 Like

Thank you very much. I knew posting here would help me with an extra pair of eyes. This is a classic programming problem. I totally forgot about the velocity_y being updated right in the body.velocity! Damn. I should get used to using temp variables. I did it for rotation since I read not to directly change any of the transforms.
After implementing the temporary velocity variable it looks like this now:

l2a52

Thank you!

1 Like

Looks good. :slight_smile:

By the way, just judging from the texture, it looks as though you’ve created a ProBuilder plane, and then changed the scale of the plane to be something other than (1,1,1). While that’s okay, I’d recommend generally using the Face/Edge tools to change the size of PB objects, rather than scaling them. It will keep your textures at high resolution. And nevermind/sorry if I’m seeing that wrong. :slight_smile:

Good eye. You are right. I did scale it up. I can increase size of PB objects using face/edge tools? I wasn’t aware of that! :slight_smile: Again thank you.

Yeah, that’s really the main strength of ProBuilder, being able to move/extrude individual faces to build up your geometry in-editor. It does the magic to keep the UVs at the same resolutions so things don’t look stretched.
5397012--547539--PB.gif

1 Like