- Home /
Few questions regarding multiplayer!
Hey guys! I've been making steady progress with my game, I have basic multiplayer functionality at the minute, by this I mean one person can host and the other players can join his game, I'm thinking maybe doing all clients connecting to a proper server would've been better but I'm not sure.
I'll go into depth a bit about my game type, It is a birds-eye Free-for-all arena game, you use click to move to control your character and will press keys to use skills to fight against other players online. There are no guaranteed hitting skills, everything must be aimed correctly whether it be a huge AoE or a small projectile.
Even though I've got this working I have a multitude of problems such as:
Movements are really laggy, not my movements of course but the other players I see are laggy (and they see me as the same!), when you're playing as the host you only really see movement lag but when you play as the person connecting you see movement + projectile lag (such as multiple projectiles showing up instead of 1) I heard that this can be fixed by applying extrapolation/interpolation but I want an extra opinion on the simplest way to make these movements smoother without having to do anything TOO complex, I'm pretty new to Unity but not programming as a whole.
Struggling with certain things when it comes to having to depend on "networkView.IsMine" e.g. I have a skill that activates when a player hits the "W" key, because of the strength of the skill I don't want players to be able to move while casting this so I do something like below on my playerattack script (which is attached to the Player prefab, along with the networkview component
but what happens is when another player is standing near me and I press "W" for the skill, they also are unable to move, here is the code for the skill itself and the code I have for stopping movement while casting the skill:
[RPC]
IEnumerator delayedRanged()
{
audio.PlayOneShot(shootSound2);
CTM.isCasting = true;
for (int i = 0; i < 40; i++)
{
myTransform.localScale *= 1.02f;
yield return new WaitForSeconds(0.05f);
}
Debug.Log("Done");
audio.PlayOneShot(shootSound3);
transform.localScale = originalScale;
clone = (Transform)Instantiate(projectile, transform.position, transform.rotation);
clone.localScale *= 20;
Physics.IgnoreCollision(clone.collider, transform.root.collider);
CTM.isCasting = false;
for (int a = 0; a < 20; a++)
{
clone.localScale /= 1.02f;
yield return new WaitForSeconds(0.05f);
}
}
/
if (networkView.isMine)
{
if (!isCasting)
{
myTransform.position = Vector3.MoveTowards(myTransform.transform.position, destination, Time.deltaTime * Speed); //move toward destination
}
}
These are my main two problems at the moment, I have a few others which I know how to fix myself but these two have me stumped, would really appreciate some tips/help, thanks!
Yes, isCasting is static, i couldn't figure out any other way to use cross-script variables, I even tried the example below from the documentation and it still spat out errors.
CT$$anonymous$$ Target; Target.isCasting = true;
Assets/Scripts/PlayerAttack.cs(110,66): error CS0120: An object reference is required to access non-static member `CT$$anonymous$$.isCasting'
and when it said this I tried to grab the component with GetComponent and it just spat out even more compile errors.
Do you think isCasting being static could be the root of the problem? I didn't think it could trip the variable on the enemy player clients.
Yep it can't be a static or if any player is casting then they all are...
Per @Fattie, statics should only be used when you really know what you are doing/need one. In network ga$$anonymous$$g it's totally fraught with problems like the one you are seeing.
Ok thanks guys! Sorry for the late reply was actually showing the game off to a few friends haha, it's co$$anonymous$$g along really well! I'll look through all these links and hopefully get something sorted :) Has anyone any advice for the laggy movement/projectiles? this is really a game breaker, I would prefer to get this fixed before I continue with anything else.
Answer by whydoidoit · Oct 08, 2012 at 08:09 PM
So for laggy movement you want to get the clients to perform the movement, fixing them up as they get more information.
For example:
When you connect to the server you record the time offset to the server's time so you have a constant "time" between them
When you move you pass the start point, the start time, the end time and the speed of movement
Clients then put the player on the path to the target using the real time each frame
The moving player then continually sends its current position as it moves
The clients fix any errors between their projected position on the track and the position reported at the particular time
That sounds perfect! Sometimes I just have a hard time planning out the whole procedure but when it's laid out like that it looks simple :)
Thanks! I'm going to go to bed now because I'm super tired but will get to work on this as soon as I get up!
Thanks again.
Click to move is one of the easiest things to make "non-laggy" - car driving/plane simulations are the hardest because you need to project the input to the clients and simulate that.
Good luck :)
Your answer
Follow this Question
Related Questions
Reducing lag in networking. 1 Answer
Problem - player networking lags 2 Answers
Network Vibration Problem 1 Answer
How can I get "!networkView.isMine" to function properly? 1 Answer
Optimize iPad multiplayer? 1 Answer