# How can I make a character instantly turn in a 2D game

So I’ve been trying to get 2 characters to always look at eachother by instantly rotating them however for some reason the opposite character always spins instead of instantly turning or at least it’s a slower. How can I fix this?

``````--> this is in a renderstep
local FacingDirection = math.floor(Opponent.Character.HumanoidRootPart.CFrame.X - Main.HRP.CFrame.X)
FacingDirection = math.sign(math.sign(FacingDirection) + 0.5)
Modules.Character:UpdateDirection(FacingDirection)
``````
``````function Character:UpdateDirection(FacingDirection) --New facing direction
Character.FacingDirection = FacingDirection

Modules.Character:StopAnimation()
Modules.Character:PlayAnimation(IdleTable[-FacingDirection])

local NewPos = Main.HRP.Position+Vector3.new(FacingDirection,0,0)
Main.HRP.CFrame = CFrame.new(Main.HRP.Position,NewPos)
end
``````

EDIT: OOPS I didn’t realize I removed the ! from the video link haha

1 Like

Well, the reason this happens seems to be latency. Each player updates their character rotation on their own client in their own renderstepped function.

That means if player 1 jumps over player 2, then instantly his own character rotates on his own screen. However, it takes a while for this to reach the server and then player2’s client. Only then can player 2 start rotating, and this also needs to travel to the server and to the client of player1 before player1 can see it. Therefore, player 1 sees a pretty huge delay, which is the problem you describe.

Consider however, the perspective of player 2: The moment he receives the signal that player 1 has jumped over him, he immediately rotates himself. The signal that player1 jumps over him includes the signal of player1 rotating, so everything looks perfectly smooth on his screen. It’s a pretty good introduction to einstein’s theory of relativity

Jokes aside, if you look closely at the video, you can tell this delay is happening, and that you were controlling the player on the right (even without looking at the name tag).

You could try to set the rotation of both players on both clients, but since the other player remains in control of his own character, it might be a bit buggy/twitchy, I’m not sure how that solution would work out. It might work. Either way, now that you understand the problem, you can work towards the best solution.

One thing is for sure: the client-server latency is not something you will be able to change. At best you can work around it and/or hide the effect. This is incidentally one of the hardest challenges to overcome to make a multiplayer pvp arcade fighting game over the net. That said, I think even this simple demo looks really cool!

Good luck!

2 Likes

Wow thanks a lot for the response I didn’t really think about it being an issue with latency I guess this kind of ruins trying to make a street fighter type game haha might jus have to accept this is the closest I can get.

Also the game is actually not a demo currently in the process of rewriting what I currently have because the code kind of poop.

Edit: Also yeah I have actually tried setting the rotation of both players on the client and uh it gets really wack

Edit 2: is it possible to use animations to make it seem like the player is instantly turning? or would I just run into the same problem

Wow, thats much more than a simple demo. That’s incredible. Did you also do the design??

Well, for a moment I thought you had already used a tween or animation. It looks a bit like that because apparently the character’s rotation is interpolated, instead of just flipping when you set a new CFrame. To be honest I’m not exactly sure about how or why this happens.

A character animation would be relative to the humanoidrootpart. Perhaps using an animation to face in the other direction instead of rotation the humanoid rootpart might work, but it would double all your fighting move animations. I’m not exactly sure, but perhaps doing this locally on both clients would make the rotation look better. However, again, the local animations to your own character would stream to the server and then to the other clients, potentially causing interference.

I can’t say if it would be a solution, it would be a bit hacky in any case.

In general, dealing with latency often involves tricks. Often it is a game of tradeoffs. On the one hand you want speed, control and accuracy, and you need to ensure a smooth and fair gaming experience and game security. But on the other hand, the slower and the less interactive with the server and other players your game is, the easier it is to hide the latency.

For example, a full client-side battle against AI doesn’t need to have latency. But even then you don’t want players to cheat, so some interaction with the server is required. Many games use ‘delayed actions’, such as an ability that charges for a moment before it actually deals damage: the game can use this charging time to already send the message to the server and other clients, to prepare for a more synchronous damage event. There is an endless supply of tricks in the industry, but pvp arcade fights are notoriously hard because the game is so centered around quick reflexes.

To block a hit, like in the old multiplayer actual arcade games where both players play on the same device, is actually really hard to do online, as there is no way around the latency. It’s almost impossible for the clients to stay synchronous, and in fact, many games focus on matching the results rather than synchrony. They create their game so it doesn’t matter if a hit lands earlier or later, as long as the outcome is the same.

Using tricks and approaches like that, it may be possible to create a smooth fighting experience over the net. There are different avenues, such as letting each player do a lot ‘by themselves’ locally (reducing the interaction but allowing smoother fighting) or just pushing everything to the server and letting the server work out what actually happens when and flatly stream that back to all clients (probably difficult to make it seem very responsive). They are all tradeoffs, aided by clever tricks and animations/effects to hide the latency. It’s a big field with interesting challenges for programmers of any level. But be careful not to dig too deep, it’s easy to get lost in complexity and limit yourself too much by the tricks you came up with.

Well that’s the best I can say about it, I think this project looks really really awesome and I really hope you will get to find nice ways to make it work out smoothly. Even without any measures against latency, I would probably want to try and play it. In any case I think you have a lot of talent and that your games will have a lot of potential. Good luck!

2 Likes

I am working on something similar, I’m making a 2.5d game where you character has to instantly turn. I figured it out and saw your post later, hopefully three years isn’t too late and can still help.

I have made one For a fast Gradual full turn that i use in my game: