How to strafe without affecting forward velocity?
I'm working on an infinite runner type game where the player character has a constantly increasing forward velocity.
I allow the player character to strafe side to side to avoid obstacles. However, this seems to reduce the player's forward velocity to about 70% of it's maximum. I do not want this to occur, as it makes my game 30% easier.
Is there any way I can allow the player character to move side to side without affecting its forward velocity? I've played around with adding forces and impulses but it doesn't seem to work very well.
I found a related thread on the UDK forum: https://forums.epicgames.com/threads/928004-Vehicle-strafe-slows-down-forward-speed, but I'm not sure how to implement this.
asked Apr 19 '15 at 01:23 PM in Blueprint Scripting
What's happening is the strafe velocity + forwardspeed is coming up against the maxwalkspeed value, so it clamps forward velocity when strafing to avoid exceeding your maxwalkspeed (so that character overall speed stays consistent).
You either need to figure out how to restrict forward speed to some value below maxwalkspeed (perhaps using the 'launch character' node with velocity override setting to drive your forward motion?), or you need to dynamically adjust the maxwalkspeed depending on the expected movement vector when you are strafing.
I would first try the launch character w/ velocity override method to see if it can be used to set your forward motion velocity every frame...the expected vector calculation and dynamic adjustment of maxwalkspeed would be a tad more complicated.
But if you want a starting point for how to do the vector calculation it would be something like this:
It would need to be done on a frame by frame basis, so you can add some nodes after your "add movement input" function for your strafe movement, since this executes every frame.
So you need to calculate the expected future velocity vector when you press a strafe key while walking forward to know the speed needed.
Calculate your forward vector: you can use "get forward vector" and multiply by the current forward speed. If your forward speed is dynamic you may need to use "get velocity" and "break vector" to pull the most recent forward speed...assuming forward is only ever along one axis.
Calculate your strafe vector: You would use the Inputaxis MoveRight axis value (it is 0 if not pressed, -1 for left strafe, 1 for right strafe) multiplied by the "get right vector". So use a [float * vector] node to calculate this. This gives the direction, but what about the magnitude?
For the magnitude of this vector in a given frame I believe the add movement input function uses the acceleration value…I believe by default it is 2048/s^2. We just want the acceleration in a given frame. You can get the time for a given frame from the "Event tick" function(delta seconds) …so I think you take [delta seconds]* [2048, or the acceleration value]. Multiply the resulting float against your strafe vector to get your expected strafe velocity vector.
Now, take the strafe velocity vector and add it to the forward velocity vector (vector + vector) to get your expected final vector. Finally, get the 'speed' of this vector use the vector length which will give you a float, and set your new maxwalkspeed to this value.
Now, a possible issue with doing this is if your forward movement is constantly trying to accelerate to fill the new maxwalk speed void, then it may increase in velocity instead remaining constant and allowing strafe velocity to increase...I guess it depends on how things are coded underneath.
When I try this I just get a faster version of the same problem :/
I've scoured the net and can't find a simply explained solution for this problem. Would really appreciate it if someone could show in Blueprints how to make this work.
answered Jan 12 '19 at 05:06 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here