Need help with 2D Networking (Part 2)

Updated on March 7, 2017 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on March 7, 2017

Hi there.

Thank you for helping me fix my code just to let the player control the character after leaving and entering another room.

Before I start with my next concern, let’s take a look at a snippet of my PlayerControls.cs script:

 private float normalizedHorizontalSpeed = 0;
 void Update()
 {
 /// Arrow keys to move
 if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
 normalizedHorizontalSpeed = -1;
 else if (Input.GetKey(KeyCode.RightArrow) || (Input.GetKey(KeyCode.D))
 normalizedHorizontalSpeed = 1;
 else if (!Input.anyKey) // No keys are pressed.
 normalizedHorizontalSpeed = 0;
 }
 void HorizontalMovement()
 {
 if (normalizedHorizontalSpeed != 0)
 {
 if (normalizedHorizontalSpeed == -1)
 {
 if (transform.localScale.x > 0)
 transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
 }
 else if (normalizedHorizontalSpeed == -1)
 {
 if (transform.localScale.x < 0)
 transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
 }
 }
 }
 

The point here is that whenever the character’s sprite is facing right and the player presses the left arrow or A key, the x-component of the scale gets reversed. This is the same as when the sprite is facing left and the player presses the right arrow or D key.

This leads to my next problem: I tried building the project to test its networking functionalities. However, the sprite’s scale doesn’t change according to the direction they are going. I tried using SyncVar to store the value of the x-component of the transform’s scale in order to synchronize the transform’s scale of the sprite, but to no avail. How is this possible?

Once again, I appreciate your prompt, detailed, and constructive feedback. Thank you!

  • Liked by
Reply
2 on March 7, 2017

Do you currently have movement working in the online environment?
If so, you must be somehow passing your transform.position values over the network, or handling it some other way.
If you are passing your transform.position, could you not also pass your transform.localScale?

on March 7, 2017

I am using “Sync Rigidbody2D” as Transform Sync Mode under the Network Transform component instead of “Sync Transform” because the movement gets laggy if I use the latter mode. Synchronizing the sprite’s scale is impossible here anyways. 🙁

Devoted
on March 7, 2017

Alright, how about having a Boolean to know which way you’re facing, passing that over the net, and running a method in the property that changes transform.localScale when that value is changed?

public bool FacingRight
{
get{
return facingRight;
}
set{
facingRight = value;
SetTransformScale(facingRight);
}
}

Show more replies
  • Liked by
Reply
Cancel