Finicky Controls

So far I have implemented some simple user controls for the actor, jumping and walking action but not at the same time. This is where my question comes in, how do I make it so that you can kinda “leap” instead of directly jumping straight up? I don’t really know if I explained my question that clear so any questions or suggestions are welcomed.

Thank you😄!

In short (and roughly speaking), a “leap” happens because the person doing the leaping has a horizontal velocity when they jump. (They may add to that by the manner of their jump, but let’s ignore that for now.)

Thus, one way of solving this is to keep track of your character’s horizontal velocity, and to update their position based on that. Implementation allowing, this should allow your character to keep moving horizontally while jumping.

If I have it correctly, you’re currently just updating the character’s position when the player presses the “forward” button. What I suggest then is that you change this such that pressing the “forward” button alters a velocity-vector, and that this vector is then used to update their position.

Put another way, this would essentially work much like your jumping does: you keep a speed for the character, and update their position based on that speed. When the user presses a button, you update that speed. But where the player may only get one jump per press, you allow the player’s horizontal velocity to change as long as they’re holding the “movement keys”.

Sounds pretty complicated. It will probably take some time speaking that I am a complete nub when it comes to this and that I would need to rewrite some stuff😅. I’m going to hit the sack soon and will be up in the morning. Until then I just have to say thanks. I’ll see you tomorrow👋

1 Like

Ok so right now as you said it just moves it by the y axis

self.pandaActor.setY(self.pandaActor, -200)

how do I do the whole change velocity vector thingy?

Thanks!:laughing:

In short, you keep a vector that stores your character’s velocity on the x- and y- axes. You then set the character’s position to be its current position plus this vector multiplied by dt (as in your jump-logic).

This essentially adds your x- and y- velocity (multiplied by dt) to your x- and y- position, respectively, and thus moves your character according to its velocity.

Now, there is one caveat: Since you’re currently moving your character relative to itself, that means that if the character turns, its velocity will turn with it–which, unless it’s what you intend for your mechanics, seems likely to feel a bit off.

You could do some additional work to rectify this, I think–but it seems to me to be likely to be easier to simply change from moving your character relative to itself to moving your character from the perspective of the world.

In this case, you should be able to do this more or less as I outlined above: instead of passing the character itself into the call to “setPos”, remove the first parameter to that method and then simply set its position as “current position + offset” (where “offset” is your velocity * dt).

Updating the velocity when the player moves is fairly straightforward: when the player presses the “forward” button, you get the character’s “forward vector” (we’ll go through how to do that when you get there, if you don’t figure it out yourself!), and add that (multiplied by an acceleration value and dt) to the velocity. And that’s it!

(Well, you’ll likely also want some sort of friction, and to limit just how far your character can go, but we’ll get to that later, I think.)

If it’s not too much, could you provide some sample code for me to visualize it or make edits so that I can understand what you mean more clearly?

Thanks again for your time😊!

Hmm…

At this point I’m tempted to just link you to the relevant lessons in my tutorial, as I cover this stuff there–with significant description, and source-code. (Not to mention the fact that the tutorial covers a number of the things that you’ve been asking about recently.) My only qualms are that the lessons in question also cover other things, and that they build on preceding lessons; in the latter case, I fear that you may be a little adrift if you just jump in several lessons into the tutorial.

So, what do you think? Shall I link you to the tutorial, or keep covering these matters piecemeal, as they come up?

Yeah… I don’t think giving a link to a tutorial mid-way is gonna be useful as it doesn’t apply to my code. All I’m asking is what should edit to my code that will do the whole velocity thing. Remember, my end goal is that you can leap.

Thank you!

You could always start from the beginning of the tutorial–it means setting your current project aside for a bit, but you may come back to it in a better position to build your intended project.

And as to what you’re asking–well, as you see above, there are a number of parts to it. “The whole velocity thing” includes, off the top of my head:

  • Storing your velocity
  • Updating it in response to player-input, which includes
    • Getting your character’s forward-vector
  • Changing how you update your character’s position
  • Applying friction (so that the player doesn’t keep moving after releasing the controls)
  • Applying a maximum velocity (so that the character doesn’t build up ridiculous speed that might break your gameplay mechanics)

It’s not a one-line change to your code.

A part of my frustration here, I suppose, is that in a sense I’ve already answered a number of your recent questions in the tutorial–including all of the above–so I feel like I’m repeating myself.

Hum.

All right, another thought: perhaps we can rig up your current code to work without using a “real” velocity-vector. Perhaps it might work if we just add a copy of your current movement-code into your jumping-task, without any checks for keys being pressed. That should move your character forward while jumping.

I’m not sure that it’ll feel good in play, but it might function.

Alright if you think it might work, let’s try it👍!!!

All right, do so, and let me know how it works! :slight_smile:

(You should have all of the code for this already, I believe.)

here is most of it I think:


        self.accept("arrow_up", self.Move)
        self.accept("arrow_up-repeat", self.Move)
        self.accept("arrow_up-up", self.stopMove)
        self.accept("arrow_right", self.Move2)
      

    def Move(self):
        self.WalkSound = self.loader.loadSfx("/Users/38167/PycharmProjects/viren/venv/walk.mp3")
        self.pandaActor.setY(self.pandaActor, -200)
        self.pandaActor.setPlayRate(2, "walk")
        walk = self.pandaActor.getAnimControl("walk")
        self.WalkSound.play()
        if not walk.isPlaying():
            self.pandaActor.loop("walk")

    def stopMove(self):
        self.pandaActor.stop("walk")

    def Move2(self):
        self.pandaActor.setH(self.pandaActor, -90)

Do you have any ideas?

I… don’t see any code related to jumping there?

Oh sorry, I thought you meant my forward thing. Here it is:

        self.jump_speed = 0
        self.gravity_force = 9.8
        self.jump_status = False
        self.accept("space", self.set_jump)
        self.taskMgr.add(self.gravity, "gravity")
        
    def set_jump(self):
        if self.jump_status == False:
            self.jump_speed = 4
            self.jump_status = True

    def gravity(self, task):
        self.pandaActor.setZ(self.box.getZ()+self.jump_speed*globalClock.getDt())
        if self.pandaActor.getZ() > 0:
            self.jump_speed = self.jump_speed - self.gravity_force*globalClock.getDt()
        if self.pandaActor.getZ() < 0:
            self.pandaActor.setZ(0)
            self.jump_speed = 0
            self.jump_status = False
        return task.cont

And, well, I’m not sure of what to say. I’ve indicated above how to implement my suggestion, and as noted, you already have all the code that’s involved with this, so… short of actually telling you to the character what to do–and again, I don’t intend to write your program for you–I’m not sure of what more to say. ^^;

I thought you were gonna tell me the other way of how to do it?

I don’t expect you to write the code for me but I was just wondering what you were talking about.

Sorry If I came off as demanding or rude I just desire your help🙏

I get that; it just sometimes feels as though you’re saying: “just please give me the code for this”–which wouldn’t be a problem in and of itself, save that it feels like it’s been happening often.

However, if I’ve misread or been impatient with you, then I apologise.

That’s what I was doing in these lines:

The above is pretty much what would be called for, I believe. Far simpler than reworking your code to use a horizontal velocity, I think–although I’m still dubious that it will feel good. But, it may be worth trying.

To clarify, since I see an ambiguity now: when I said “movement code”, I meant literally the line of code that updates the position of your character. i.e. The code that produces movement.

Ah ok, let me try it and I’ll send any errors or problems. Thank you!

1 Like

Not a problem! Good luck with it! :slight_smile:

Correct me if I’m wrong here, but I tried putting

  self.pandaActor.setY(self.pandaActor, -200)

inside of the set_jump function as it wouldn’t make sense to me to put it inside gravity.

It looked like this:

 def set_jump(self):
        if self.jump_status == False:
            self.jump_speed = 4
            self.jump_status = True
            self.pandaActor.setY(self.pandaActor, -200)

Is there something wrong because it won’t make the panda leap?

Thanks again!