Horizontal & vertical movement done, but what about fly/fall?

I’m moving ahead of myself here, but just curious:

Apart from X-axis and Z-axis movement, If I wanted to implement player controls for the Player block to fly/ascend and fall/descend freely as well, would that even be possible through the method Rick takes us through?

Through this episode’s method, I could make the block fly up freely through eg.:

float yValue = Input.GetAxis("Jump");

…but it won’t fall or be able to descend in a controlled manner.

Would I have to make some kind of custom keys in the script instead?

Hi Nukem,

First of all, you have to define what you mean by “falling” because we must not forget that Unity’s physics simulation is also working on our player.

The problem with Rick’s code is that we manipulate the transform.position directly. We override the values that were applied by the physics simulation.

The “correct” way to move a game object within the physics simulation is to use the Rigidbody and call, for example, AddRelativeForce on it. Since I understood that you would like to challenge yourself, you could look that method up in the Unity API, and try to implement it in your game. It’s not difficult.

In the Project Boost game, Rick uses the AddRelativeForce method to make his rocket fly.

If you just want to make your player fly and ignore the “physics”, your idea is the way to go. Maybe rename the “Jump” axis to “Fly” in the Unity Settings, and rename it accordingly in your code, so when you read your code in a few weeks, you immediately understand what it is supposed to do.

Whether you have to make custom keys depends on your idea. For example, if you want to use the spacebar for the “flying”, you could check against that value via Input.GetKey. You don’t have to implement a new “axis” in the Unity Settings.

Did this help? :slight_smile:


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms